Javascript/PHP string equality -


i'm running ajax call has success function takes in variable returned php page so.

ajax:

$.ajax ({       type: "post",     url: "loginrequest.php",     data: 'username=' + username + '&password=' +pass,     success: function(html){         console.log(html); // returns login         console.log(typeof html); // returns string         console.log(html === "login"); //returns false          if(html === 'login'){             window.location.href = 'index.php';         }         else if(html === 'false'){             alert("login failed");         }     } }); 

php:

if($count == 1){     $_session['user'] = $myusername;     $return = "login";     echo json_encode($return); } else {     $return = "false";     echo json_encode($return); } 

as can see i'm trying implement simple login page , redirect user or display alert depending on outcome of number of rows returned database query.

what don't understand this:

console.log(html); // returns "login" console.log(typeof html); // returns string console.log(html === "login"); //returns false 

i tried echo-ing without json_encode() , still give me same results. using == read it's safer use === switched still won't return true.

you're sending json, means you're sending literal bytes:

"login" "false" 

note quotes in there. js code either needs decode json, or compare raw json itself:

 result = json.parse(html)  if (result == "login") 

or

 if (html == '"login"')   // note quotes 

a simple console.log(html) have shown you're dealing with.


Comments

Popular posts from this blog

java - Static nested class instance -

c# - Bluetooth LE CanUpdate Characteristic property -

JavaScript - Replace variable from string in all occurrences -