I used this jquery function to send a request to abc.php; The value of x returned is correct but whatever be it value, only the if is executed; even if x is false, else 开发者_开发技巧is not executed.
$.post('abc.php',{u:e1.value},function(x){
document.getElementById('d').innerHTML+=" x="+x;
if(x){
document.getElementById('d').style.color="green";
document.getElementById('d').innerHTML+="<img src='t.jpeg'>";
q=true;
}
else{
document.getElementById('d').style.color="red";
document.getElementById('d').innerHTML+="U";
q=false;
}document.getElementById('f').innerHTML+=" q="+q;
});
}
This is the file abc.php;
<?php
$db = "b";
$link = mysql_connect("localhost","root","");
mysql_select_db($db, $link) or die(mysql_error());
$q = "select * from us where ad='$_POST[u]'";
$r=mysql_query($q, $link) or die(mysql_error());
if(mysql_num_rows($r)==1)
echo true;
else echo false;
mysql_close($link) or die(mysql_error());
?>
The data you are checking is a string not a boolean.
You test should be: x === "true"
(although you may need to account for whitespace in the output of the PHP.
I think the issue is you're echoing out the variable so the data is seen as a string therefore if it is "true" or "false" the variable is always set, meaning x is true.
Try changing your statement to if(x.toLowerCase() == "true")
You can add a further = to check it is indeed a string
The data is taken as string, not boolean. and moreover you are echoing booleans instead of strings .
http://codepad.org/KCROfdqq
<?php
echo "true"; // output : true
echo true ; // output : 1
?>
So try these changes :
PHP:
echo "true" ;
and echo "false";
Javascript:
if(x == 'true')
精彩评论