简体   繁体   English

使用javascript和php启用和禁用按钮

[英]enabling and disabling a button using javascript and php

hi i really dont know a lot about javascript so sorry for this amatuer question. 嗨,我真的对javascript不太了解,所以很抱歉这个amatuer问题。 im trying to make a quiz page using php with 2 sets of questions. 即时通讯试图使用带有2组问题的php制作测验页面。 i want to disable the 2nd button for the other quiz and only be enabled when the user passed the first one. 我想为其他测验禁用第二个按钮,并且仅在用户通过第一个测验时才启用。

this is what i wanted to happen: 这就是我想发生的事情:

<?php

require 'config.php';
$name = "kevin";
$result =mysql_query("select score from members where Username = '$name' " );

?>

<input name="button1" type="button" value="1" />
<input name="button2" type="button" value="2" disabled="disabled" />
<script type="text/javascript">

  if<?php $score is greater than  5 ?>{
         $('#button2').removeAttr('disabled');
    }
    else {
        $('#button2').attr("disabled","disabled");   

    }
});
</script>

any help will be much appreciated, thanks in advance. 任何帮助将不胜感激,在此先感谢。

You're trying to mix PHP and JavaScript, you can't do that. 您正在尝试混合使用PHP和JavaScript,但您不能这样做。 They run on completely different machines at completely different times. 它们在完全不同的时间在完全不同的计算机上运行。

Since the score is known server-side (in PHP) before the page is even sent to the client, make the conditional happen in the PHP code and only emit to the client the markup that you want. 由于分数是在页面甚至发送给客户端之前就已经知道的服务器端分数(在PHP中),因此有条件的发生在PHP代码中,并且仅向客户端发出所需的标记。 Something like this: 像这样:

<?php

require 'config.php';
$name = "kevin";
$result =mysql_query("select score from members where Username = '$name' " );

?>

<input name="button1" type="button" value="1" />
<?php
    // use a real conditional, I'm just duplicating you for simplicity
    if ($score is greater than  5) {
?>
    <input name="button2" type="button" value="2" />
<?php
    } else {
?>
    <input name="button2" type="button" value="2" disabled="disabled" />
<?php
    }
?>

There's no need for JavaScript at all in this case, since the logic is all server-side. 在这种情况下,根本不需要JavaScript,因为逻辑是服务器端的。

Now you can still use JavaScript for more client-side interactions of course. 现在,您当然仍然可以使用JavaScript进行更多的客户端交互。 Also, be aware that this isn't really any kind of security measure to prevent users from clicking on the button. 另外,请注意,这并不是阻止用户单击按钮的真正安全措施。 Any user can modify the HTML/JavaScript being sent to them and still click on the button if they really want to. 任何用户都可以修改发送给他们的HTML / JavaScript,如果他们确实愿意,还可以单击按钮。 So if you need to prevent the user from doing something then that would need to be done server-side as well, in response to whatever action the user should not be authorized to perform. 因此,如果您需要阻止用户执行某项操作,则也应在服务器端完成此操作,以响应不应授权用户执行的任何操作。

You can try like this 你可以这样尝试

if($score > 5){
?>
    <script>
    $(document).ready(function(){
        $('#button2').removeAttr('disabled');
    });
    </script>
<?
}else{
?>
    <script>
    $(document).ready(function(){
        $('#button2').attr('disabled');
    });
    </script>
<?
}

or if removeAttr/attr are not working then you can do like this 或者,如果removeAttr / attr无法正常工作,则可以这样做

$('#button2').prop('disabled', false);
$('#button2').prop('disabled', true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM