AJAX PHP条件逻辑无法正确评估 [英] AJAX PHP Conditional Logic not evaluating correctly

查看:71
本文介绍了AJAX PHP条件逻辑无法正确评估的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于某种原因,我来自PHP的ajax.responseTest在下面的javascript条件代码中无法正确评估。 ajax.responseTest返回为 signup_success(我用一个警告框对其进行了检查,它肯定会正确返回),但是我的JS代码中的条件将无法正确评估。

For some reason my ajax.responseTest coming from my PHP will not evaluate correctly in the javascript conditional code I have below. The ajax.responseTest is coming back as "signup_success" (I checked it with an alert box and it is definitely coming back correctly), but the conditional in my JS code will not evaluate correctly.

这是JavaScript:

Here is the javascript:

else {
    _("signupbtn").style.display = "none";
    status.innerHTML = 'please wait ...';
    var ajax = ajaxObj("POST", "signup.php");
    ajax.onreadystatechange = function() {
        if(ajaxReturn(ajax) == true) {
            if(ajax.responseText != "signup_success"){
                status.innerHTML = ajax.responseText;
                _("signupbtn").style.display = "block";
            } else {
                _("sign").innerHTML = ajax.responseText;
                window.scrollTo(0,0);
                _("signupform").innerHTML = "OK "+u+", check your email inbox and junk mail box at <strong>"+e+"</strong> in a moment to complete the sign up process by activating your account. You will not be able to do anything on the site until you successfully activate your account.";
            }
        }
    }
ajax.send("u="+u+"&e="+e+"&p="+p1+"&g="+g);
}

如果我在 ajax.responseText!= signup_success中更改逻辑运算符到 ==,else块将运行,因此在条件逻辑之前没有其他问题,我已经对其进行了测试,并且PHP(以下)肯定会返回 signup_success。您认为问题出在 if语句中?

If I change the logical operator in "ajax.responseText != "signup_success" " to "==" the else block will run, so there's not some other problem prior to the conditional logic, and I have tested it and the PHP (below) is DEFINITELY returning "signup_success". What do you think the problem could be in that "if" statement?

以下是PHP供参考:

    <?php
// Ajax calls this REGISTRATION code to execute
if(isset($_POST["u"])){
    // CONNECT TO THE DATABASE
    include_once("php_includes/db_conx.php");
    // GATHER THE POSTED DATA INTO LOCAL VARIABLES
    $u = preg_replace('#[^a-z0-9]#i', '', $_POST['u']);
    $e = mysqli_real_escape_string($db_conx, $_POST['e']);
    $p = $_POST['p'];
    $g = preg_replace('#[^a-z]#', '', $_POST['g']);
    // GET USER IP ADDRESS
    $ip = preg_replace('#[^0-9.]#', '', getenv('REMOTE_ADDR'));
    // DUPLICATE DATA CHECKS FOR USERNAME AND EMAIL
    $sql = "SELECT id FROM users WHERE username='$u' LIMIT 1";
    $query = mysqli_query($db_conx, $sql); 
    $u_check = mysqli_num_rows($query);
    // -------------------------------------------
    $sql = "SELECT id FROM users WHERE email='$e' LIMIT 1";
    $query = mysqli_query($db_conx, $sql); 
    $e_check = mysqli_num_rows($query);
    // FORM DATA ERROR HANDLING
    if($u == "" || $e == "" || $p == "" || $g == ""){
        echo "The form submission is missing values.";
        exit();
    } else if ($u_check > 0){ 
        echo "The username you entered is alreay taken";
        exit();
    } else if ($e_check > 0){ 
        echo "That email address is already in use in the system";
        exit();
    } else if (strlen($u) < 3 || strlen($u) > 16) {
        echo "Username must be between 3 and 16 characters";
        exit(); 
    } else if (is_numeric($u[0])) {
        echo 'Username cannot begin with a number';
        exit();
    } else {
    // END FORM DATA ERROR HANDLING
    // Begin Insertion of data into the database
    // Hash the password and apply your own mysterious unique salt
    $p_hash = md5($p);
    // Add user info into the database table for the main site table
    $sql = "INSERT INTO users (username, email, password, gender, ip, signup, lastlogin, notescheck) VALUES('$u','$e','$p_hash','$g','$ip',now(),now(),now())";
    $query = mysqli_query($db_conx, $sql); 
    $uid = mysqli_insert_id($db_conx);
    // Establish their row in the useroptions table
    $sql = "INSERT INTO useroptions (id, username, background) VALUES ('$uid','$u','original')";
    $query = mysqli_query($db_conx, $sql);
    // Create directory(folder) to hold each user's files(pics, MP3s, etc.)
    if (!file_exists("user/$u")) {
        mkdir("user/$u", 0755);
    }
    //Email the user their activation link
    //$to = "$e";    
//  $from = "auto_responder@dilingle.com";
//  $subject = 'yoursitename Account Activation';
//  $message = '<!DOCTYPE html><html><head><meta charset="UTF-8"><title>Dilingle Message</title></head><body style="margin:0px; font-family:Tahoma, Geneva, sans-serif;"><div style="padding:10px; background:#333; font-size:24px; color:#CCC;"><a href="http://www.dilingle.com"><img src="http://www.yoursitename.com/images/logo.png" width="36" height="30" alt="yoursitename" style="border:none; float:left;"></a>yoursitename Account Activation</div><div style="padding:24px; font-size:17px;">Hello '.$u.',<br /><br />Click the link below to activate your account when ready:<br /><br /><a href="http://www.yoursitename.com/activation.php?id='.$uid.'&u='.$u.'&e='.$e.'&p='.$p_hash.'">Click here to activate your account now</a><br /><br />Login after successful activation using your:<br />* E-mail Address: <b>'.$e.'</b></div></body></html>';
//  $headers = "From: $from\n";
//        $headers .= "MIME-Version: 1.0\n";
//        $headers .= "Content-type: text/html; charset=iso-8859-1\n";
//  mail($to, $subject, $message, $headers);
    echo "signup_success";
    exit();
    }
exit();
}
?>

就像我说的那样,PHP脚本末尾的 signup_success肯定会触发,但是上面的JS无法正确评估它。

Like I said, the "signup_success" at the end of the PHP script is definitely firing, but the JS above will not evaluate it correctly.

预先感谢!

推荐答案

假设参考PHP代码正确发布,您的脚本中就有空白:

Assuming that the reference PHP code is posted correctly, you have has leading whitespace in your script:

    <?php
^^^^

输出为:

    signup_success

尝试删除不必要的空格从脚本中删除或修剪responseText:

Try removing unnecessary whitespace from your script or trim the responseText:

if(ajax.responseText.replace(/^\s+|\s+$/g, "") == "signup_success") ...

这篇关于AJAX PHP条件逻辑无法正确评估的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆