cihangir ND cihangir ND - 6 months ago 10
jQuery Question

Cant make comparison with php echo output in javascript (jquery-post)

I enter password in password field, and send it from javascript to php, and compare it with the '123' key variable in php. So if it is true it echo outs true, otherwise false.

Then, in my javascript file at callback function field, the data can not be used in if statement. I dont know why.

However, when I just want to write data it writes true in my html file, but i cant make comparison with this 'true'

My javascript:

$.post('validation.php', { input: pass }, function(data) {
if ( data === "true")
{
// even if it is true, It skips here, goes else
}
else
{

}
});


My php file is:

<?php

$key = '123';
if (isset($_POST['input'])) {
$pass = $_POST['input'];
if ($pass === $key)
{
echo 'true';
}
else
{
echo 'false';
}
}
?>

Answer

you might need to trim the returned value when comparing it in your javascript:

...if(data.trim() === "true")...

or

 var option = data.trim();
 if(data.trim() === option)...
Comments