简体   繁体   English

AJAX无法从PHP读取回显数据?

[英]AJAX cannot read echo data from PHP?

I've been testing my register, and to an extent the code works. 我一直在测试我的寄存器,并且在某种程度上可以工作。 But it seems to not be able to compare the text from the PHP script. 但是它似乎无法比较PHP脚本中的文本。

I tried using existing email AND username, existing email, existing username and two non-existing username and email. 我尝试使用现有的电子邮件和用户名,现有的电子邮件,现有的用户名和两个不存在的用户名和电子邮件。 But all give me the echoed data + Fail (which is the end conditional statement in JQuery). 但是所有这些都会给我回显的数据+ Fail(这是JQuery中的结束条件语句)。

JQuery: JQuery的:

$( function ajaxRegCheck() {
    $('#reg_form').submit(function(e) {
        e.preventDefault();
        $.ajax({
            type: "POST",
            url: 'save_register.php',
            data: $(this).serialize(),
            success: function(data)
            {

                if(data == "un_em_exists"){
                    alert(data+" Username and Email exists.");
                } else if(data == "un_exists") {
                    alert(data+" Username exists.");
                } else if(data == "em_exists") {
                    alert(data+" Email exists.");
                } else if(data == "success"){
                    alert(data+" Created account.");
                } else {
                    alert(data+ " Fail.");
                }
            } 
        });
    });
});

PHP Register: PHP注册:

if(($exist_check_user->rowCount() > 0) AND ($exist_check_em->rowCount() > 0)){
    echo "un_em_exists";
} else if($exist_check_user->rowCount() > 0) {
    echo "un_exists";
} else if($exist_check_em->rowCount() > 0) {
    echo "em_exists";
} else {
    $sql = "INSERT INTO Users (username, email, password) VALUES (:r_un, :r_em, :r_pw)";
    $q = $cdb->prepare($sql);
    $q->execute(array(':r_un'=>$reg_username,':r_em'=>$reg_email,':r_pw'=>$reg_pw));
    echo "success";
}

I do not why it skips the if statements in the JQuery and go straight to the last else condition. 我不理解为什么它会跳过JQuery中的if语句并直接转到最后的else条件。 It clearly outputs the string from the PHP side correctly, but it doesn't seem to be able to compare the string? 显然,它可以正确地从PHP端输出字符串,但是似乎无法比较该字符串?

For example, if I register with existing username and email, it'll echo 'un_em_exists' response. 例如,如果我使用现有的用户名和电子邮件注册,它将回显“ un_em_exists”响应。 On the JQuery the alert matches this as it says, "un_em_exists Fail.", which is the last statement. 在JQuery上,警报与其匹配,因为它说的是“ un_em_exists Fail。”,这是最后一条语句。

UPDATE: 更新:

Found the problem, but not too sure how to fix. 找到了问题,但不太确定如何解决。 I tested the string length, and in return I get string length 16 (testing from the JQuery side), and on PHP I get 12 - which is the correct amount for "un_em_exists". 我测试了字符串长度,作为回报,我得到了字符串长度16(从JQuery端进行测试),在PHP上,我得到了12-这是“ un_em_exists”的正确值。

I do not know what the extra 4 amounts come from though. 我不知道这4个额外的金额是多少。

成功使用data.trim()整理数据后,可能会有空白

Make sure the returned value is text like so: 确保返回的值是这样的文本:

$( function ajaxRegCheck() {
    $('#reg_form').submit(function(e) {
        e.preventDefault();
        $.ajax({
            dataType: 'text',
            type: "POST",
            url: 'save_register.php',
            data: $(this).serialize(),
            success: function(data)
            {

                if(data == "un_em_exists"){
                    alert(data+" Username and Email exists.");
                } else if(data == "un_exists") {
                    alert(data+" Username exists.");
                } else if(data == "em_exists") {
                    alert(data+" Email exists.");
                } else if(data == "success"){
                    alert(data+" Created account.");
                } else {
                    alert(data+ " Fail.");
                }
            } 
        });
    });
});

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

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