简体   繁体   English

我如何在我的jquery中检查php的返回值是“ true”还是“ false”?

[英]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: 在php网站上,我有一个选择查询和一个末尾的语句:

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. 现在在我的jquery代码中,基于返回的值,我是否要显示警报。 So I'm doing ajax call: 所以我正在做ajax电话:

(...)
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. 即使我的php脚本打印出的是true,我也从未在jquery中看到警报“ here”。 Why? 为什么?

There is extra whitespace in the php output. php输出中有多余的空格。 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 一个更实用的解决方案是始终使用json

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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