cihangir ND cihangir ND - 1 year ago 66
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


My php file is:


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

Answer Source

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

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


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