I am working on php and javascript project. I just want to compare echo return value of php with javascript variable.
php backend code which returns ‘no’ using echo
if($connection){ $sql ="SELECT secondname FROM account WHERE email = '$email' && password = '$password'"; $searchquery = mysqli_query($connection, $sql); if(!(mysqli_num_rows($searchquery) == 0)) { $row = mysqli_fetch_array($searchquery); $secondname = $row['secondname']; echo $secondname ; } else { echo 'no'; }
Now comparing with javascript variable
$.post("signnin.php",{ email: email, password: password, }, function(data, status){ if(data == 'no'){ console.log('same'); }else{ console.log('not same'); } });
it give same result if value are same or not. i also JSON.stringify but it still not working
Advertisement
Answer
The output from the URL probably includes white space.
JSON is a good way to normalize that, but you need to apply it at the PHP end, not the JavaScript end.
$secondname = $row['secondname']; header("Content-Type: application/json"); echo json_encode([ "secondname" => $secondname ]); } else { header("Content-Type: application/json"); echo json_encode([ "failure" => "Login failed" ]); }
and then:
$.post( "signnin.php", { email, password }, function(data, status){ if(data.failure){ console.log('same'); } else { console.log('not same'); } } );