I am sending a post request to a PHP script via AJAX like so:
$.post("login.php", {u: username,p: password})
.done(function(response){
console.debug(response);
if (response === "0") {
$("#loginMessage").html("Invalid Username or Password");
} else {
$.mobile.changePage("#homePage");
}
})
And login.php snippet. If the login credential fails, I am echoing "0". I believe this would output a string, since it is in quotes, as opposed to echo 0;
if ($stmt->num_rows > 0 && password_verify($password,$hashedPassword)) {
$_SESSION["uid"] = $uid;
} else {
echo "0";
}
However, when I enter invalid credentials the comparison response === "0" is returning FALSE, although when I use console.debug(response), it is showing that it is indeed 0.
After some of my own fiddling, I found that if I change response === "0" to response == 0, it correctly evaluates to true.
response === 0 also evaluates to FALSE, which confuses me even more.
Can someone explain this a bit for me?
0from the ajax response into an integer.0 === "0"is false in JS, because the types don't match (string v.s. integer).===checks for equality in value but also in the type of the variable.console.log(response.length)tell you?<?php ... ?>delimiters, they will be included in the response. I assume the response you get is actually0\nor something similar.