Return true / false in javascript from php file

I use the $ .post method to call ajax.
I have a script (php) that checks for a user in the database and returns (echo's) 1 if it exists, and 0 if not. Is it possible to return true and false so that javascript recognizes it as logical?

+5
source share
4 answers

http://api.jquery.com/jQuery.post/

mentions the data analysis option as JSON, so theoretically you can use php to echo data in json form.

+1
source

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

if (data == '1') {
   //it true
} else {
   // it false
}
+3
source

"0" false JavaScript, "1" true, . === .

+3
source

As far as I remember, js accepts 0 as false.

So you can use

if (ans) ...; else ...

I'm not sure, but try it!

0
source

All Articles