简体   繁体   中英

How can I check in my jquery if the return value from php is 'true' or 'false'?

On the php site I have a select query and a statement at the end:

if ($stmt->num_rows >=1) {
        // A user with this email address already exists
        echo "false";
    }
    else{
        echo "true";
    }

Now in my jquery code, based on the value returned, I want to display alert or not. So I'm doing ajax call:

(...)
dataType:'text',
                    success: function(ans)
                    {
                        var data = ans;
                        if (ans ==='true'){

                            alert("here");
                        }
                        else{

                          alert("else");
                        }

                }});
(...)

And even though my php script prints out true, I never see the alert "here" in my jquery. Why?

There is extra whitespace in the php output. You can resolve this by trimming that whitespace

Change:

 var data = ans;

To

 var data = $.trim(ans);

A more practical solution is to always use json

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM