Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return true/false to javascript from php file

Tags:

javascript

php

I'm using $.post method to make ajax calls.
I have a script (php) that checks for user existing in database and returns (echo's) 1 if exist and 0 if not. Is it possible to return true and false so javascript recognize it as boolean ?

like image 656
Nick Avatar asked Jan 07 '11 14:01

Nick


2 Answers

No, the values will always be returned as text. You need to compare the values in your JavaScript.

if (data == '1') {
   //it's true
} else {
   // it's false
}
like image 153
David Powers Avatar answered Oct 04 '22 23:10

David Powers


Actually the string "0" is interpreted as false by JavaScript and "1" is interpreted as true, so you can simply work with the string value as a boolean. But then you can't use === to compare.

like image 33
Floern Avatar answered Oct 04 '22 23:10

Floern