json_encode boolean returning wrong value

11,554

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).

Share:
11,554
RapsFan1981
Author by

RapsFan1981

Updated on June 08, 2022

Comments

  • RapsFan1981
    RapsFan1981 almost 2 years

    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);
    }
    ...
    ?>
    
    • Mahn
      Mahn almost 12 years
      Is json_encode actually used somewhere, or are just collecting that echo "true"; as the ajax response?
    • RapsFan1981
      RapsFan1981 almost 12 years
      Please see my edit. This is what I was originally trying but it kept returning null no matter what I did so I figured trying a simple string or boolean check would be easier, and once I got that working then I'd try this again.
    • Ry-
      Ry- almost 12 years
      @RapsFan1981: See updated answer.
  • Mahn
    Mahn almost 12 years
    "never write == true" -> why?
  • Ry-
    Ry- almost 12 years
    @Mahn: It only leads to code errors. if(data == true) is usually equivalent to if(data). If it is equivalent, the == true is superfluous and should be taken out. If it's not equivalent, you have a serious problem with your code - as is the case here. At most use === true if you really need to check for a true value and nothing else.
  • Mahn
    Mahn almost 12 years
    Hm, it may be just me, but I'm having a hard time imagining in what cases could if(data == true) not be equivalent to if(data), could you give an example? just curious.
  • RapsFan1981
    RapsFan1981 almost 12 years
    No effect. alert("false"); is still firing.
  • Ry-
    Ry- almost 12 years
    @Mahn: Well, JavaScript. 'true' != true but 'true' is truthy.
  • Ry-
    Ry- almost 12 years
    @RapsFan1981: You're doing the == "true" thing again. Take it out.
  • RapsFan1981
    RapsFan1981 almost 12 years
    $json = array('boolean' => true, 'jsonUser' => $myusername); if(data.boolean == true) but doesn't work
  • Ry-
    Ry- almost 12 years
    @RapsFan1981: Don't do == true either. Take it out. Now, which browser are you using?
  • Ry-
    Ry- almost 12 years
  • Mahn
    Mahn almost 12 years
    Did you manage to get it working? I believe you need either if(data) or if(data.boolean), and additionally perhaps setting an application/json header on the php side.
  • RapsFan1981
    RapsFan1981 almost 12 years
    Stil not working, minitech has been helping me. I've tried if(data) and if(data.boolean).
  • RapsFan1981
    RapsFan1981 almost 12 years
    after a lot of experimentation and changing things I got it going. I made a working registration script that uses .post so I ended up using .post for my login as well. I also added e.preventDefault() as suggested by minitech.