Nick
Nick

Reputation: 16105

Return true/false to javascript from php file

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 ?

Upvotes: 5

Views: 8780

Answers (4)

simon
simon

Reputation: 1191

As far as I remember, js takes 0 like a false.

So you can use

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

I'm not sure, but try it!

Upvotes: 0

please delete me
please delete me

Reputation:

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

mentions an option to parse data as JSON, so in theory you could use php to echo data in json form.

Upvotes: 1

Floern
Floern

Reputation: 33914

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.

Upvotes: 3

David Powers
David Powers

Reputation: 1664

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
}

Upvotes: 3

Related Questions