AJAX发回整个页面而不是结果

时间:2014-06-30 22:24:39

标签: javascript php html ajax

我在弄清楚为什么我的ajax响应向我发送整个页面时遇到了一些麻烦。 我没有使用传统的AJAX代码,因为这就是我在跟踪的教程中这样做的原因。我正在使用firebug来跟踪我的所有javascript变量,当我在responseText var中断代码时它会给我整个页面。以下是我正在使用的页面上的javascript代码:

function changepass() {
  var u = _("username").value;

  var cp = _("currentPass").value;

  var np = _("newPass").value;

  var cnp = _("confirmNewPass").value;

  if(np != cnp) {
    _("status").innerHTML = "The passwords given do not match!";
  } else if (cp === "" || np === "" || cnp === "") {
    _("status").innerHTML = "Please fill out all of the fields.";
  } else {
    _("changepassbtn").style.display = "none";
    _("status").innerHTML = 'please wait ...';
 var ajax = ajaxObj("POST", "change_password.php");
        ajax.onreadystatechange = function() {
       if(ajaxReturn(ajax) == true) {
var response = ajax.responseText;
if(response == "success"){
_("status").innerHTML = "Your password change was successful! Click the button below to proceed.<br><br><a href=\"user.php\" style=\"font-size:\'1.5em\'\">Back To Home Page</a>";
} else if (response == "no_exist"){
_("status").innerHTML = "Your current password was entered incorrectly.";
    _("changepassbtn").style.display = "initial";
} else if(response == "pass_failed"){
_("status").innerHTML = "Change password function failed to execute!";
    _("changepassbtn").style.display = "initial";
} else {
_("status").innerHTML = "An unknown error occurred";
    _("changepassbtn").style.display = "initial";

}
       }
        }
        ajax.send("u="+u+"&cp="+cp+"&np="+np+"&cnp"+cnp);
}
}

这是我用来处理请求的AJAX代码。

  function ajaxObj( meth, url ) {
        var x = new XMLHttpRequest();
        x.open( meth, url, true );
        x.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        return x;
    }

    function ajaxReturn(x){
        if(x.readyState == 4 && x.status == 200){
           return true; 
        }
    }

非常感谢任何帮助。

谢谢!

这是服务器端PHP:

if(isset($_POST["u"]) && isset($_POST['oe']) && isset($_POST['ne']) &&    isset($_POST['p'])) {
        $oe = mysqli_real_escape_string($db_conx, $_POST['oe']);
        $ne = mysqli_real_escape_string($db_conx, $_POST['ne']);
        $p = md5($_POST['p']);      
        $u = mysqli_real_escape_string($db_conx, $_POST['u']);
        var_dump($oe, $ne, $p, $u);
        $sql = "SELECT username, password, email FROM users WHERE username='$u' LIMIT   1";
        $query = mysqli_query($db_conx, $sql);
        $row = mysqli_fetch_row($query);        
        $db_username = $row[0];
        $db_password = $row[1];
        $db_email = $row[2];
        var_dump($db_username, $db_password, $db_email);
        if($db_email != $oe) {
            echo "bad_email";
            exit();
        } else if($db_password != $p) {
            echo "no_exist";
            exit();
        } else {
            $sql = "UPDATE users SET email='$ne' WHERE username='$db_username' LIMIT 1";
            $query = mysqli_query($db_conx, $sql);
            $sql = "SELECT email FROM users WHERE username='$db_username' LIMIT 1";
            $query = mysqli_query($db_conx, $sql);
            $row = mysqli_fetch_row($query);
            $db_newemail = $row[0];
            if($db_newemail == $ne) {
                echo "success";
                exit();
            } else {
                echo "email_failed";
                exit();
    }
    }
}

我的错误是一个简单的语法错误。 Gosh PHP很挑剔!该错误发生在ajax.send命令中。我在最后一个参数上错过了'='。

感谢您的帮助!

0 个答案:

没有答案