doushouxie7064 2012-07-24 20:56
浏览 97
已采纳

json_encode布尔返回错误的值

I'm writing a jquery login. If the php login script (checklogin.php) echoes "true" then my jquery page is supposed to give an alert. When I run a login.php (that doesn't use jquery) it sends the request to checklogin.php which echoes "true" however if I use my jquery login script to send the login info to checklogin.php, the alert I trace back says "false".

$('#login').click(function(e){
      $.getJSON("http://www.hedonsoft.com/game/login.php",{username: $("#username").val(), password: $("#password").val()},function(data){
         if(data==true){
            alert(data.boolean);
            //$('#logindiv').slideUp();
            //$('#gamediv').show(); 
            //$('#registerdiv').hide();
            //$('#gameheader').html(data['username']);
         }else{
            alert("false");
         }
      });
});

<?php
header('Access-Control-Allow-Origin: *');
$host="localhost"; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name="hedonsof_conflict"; // Database name 
$tbl_name="members"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form 
$myusername=$_POST['myusername']; 
$mypassword=$_POST['mypassword']; 

// To protect MySQL injection (more detail about MySQL injection)
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){

// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword"); 
$json = array('boolean' => 'true', 'jsonUser' => $myusername);
echo "true";
}
else {
echo "Wrong Username or Password";
}
?>

So long story short, when I try to login from a regular php page the value I trace is true, when I try to login from a jquery page the value I trace is false;

EDIT:

<?php
...
$sql="SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){

// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword"); 
$json = array('boolean' => true, 'jsonUser' => $myusername);
echo true;
}
else {
echo "Wrong Username or Password";
}
?>

And my jQuery...

$('#login').click(function(e){
      $.getJSON("http://www.hedonsoft.com/game/login.php",{username: $("#username").val(), password: $("#password").val()},function(data){
         if(data){
            alert("true");
            //$('#logindiv').slideUp();
            //$('#gamediv').show(); 
            //$('#registerdiv').hide();
            //$('#gameheader').html(data['username']);
         }else{
            alert(data); // shows null
         }
      });
});

With json_encode, boolean's value is returned null

$('#login').click(function(e){
       $.getJSON("http://www.hedonsoft.com/game/login.php",{username: $("#username").val(), password: $("#password").val()},function(data){
          if(data.boolean == "true"){
             alert("true");
             //$('#logindiv').slideUp();
             //$('#gamediv').show(); 
             //$('#registerdiv').hide();
             //$('#gameheader').html(data['username']);
          }else{            alert(data);
          }
       }); });
<?php
...
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("myusername");
session_register("mypassword"); 
$json = array('boolean' => true, 'jsonUser' => $myusername);
echo json_encode($json);
}
...
?>
  • 写回答

1条回答 默认 最新

  • douyi6960 2012-07-24 20:57
    关注

    Use true, not 'true'. 'true' is a string, and you want a boolean:

    $json = array('boolean' => true, 'jsonUser' => $myusername);
    

    Also, never write == true.

    if(data==true) {

    Okay, now the problem is:

    $json = array('boolean' => true, 'jsonUser' => $myusername);
    echo true;
    

    Don't echo true, echo json_encode($json).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大