使用JavaScript进行密码更改时未收到任何响应

时间:2014-07-26 14:21:28

标签: javascript php

下面是我更改密码的脚本。如果所有数据都输入正确,请等待消息,但没有收到进一步的处理。请帮我纠正错误。 如果我在没有javascript的情况下处理这个脚本,它可以正常工作,但是使用javascript它会变得没有响应。

<?php
include_once("check_login_status.php");
if(isset($_GET["u"])){
if(isset($_POST["email"])){
    include_once("php_includes/db_conx.php");
    $email = mysqli_real_escape_string($db_conx, $_POST['email']);
    $pass = md5($_POST['pass']);
    $pass1 = md5($_POST['pass1']);
    $pass2 = md5($_POST['pass2']);
    if($email == "" || $pass == ""||$pass1 == ""||$pass2 == ""){
        echo "login_failed";
        exit();
    } else  if($pass1 !=$pass2){echo"no match";
        exit();}

    else    
 {
    // END FORM DATA ERROR HANDLING
        $sql = "SELECT id, username, password FROM users WHERE email='$email'";
        $query = mysqli_query($db_conx, $sql);
        $row = mysqli_fetch_row($query);
        $db_id = $row[0];
        $db_username = $row[1];
        $db_pass_str = $row[2];
        if($pass != $db_pass_str){      
            echo "data wrong";
            exit();
        } else if($u != $db_username){
            echo "username_mismatched";
            exit();
        }
        else {
            $sql = "UPDATE users SET password='$pass1' WHERE email='$email'";
            $query = mysqli_query($db_conx, $sql);
            echo "success";
            exit();
            }
    }
    exit();
}}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script src="js/main.js"></script>
<script src="js/ajax.js"></script>
<script>
function emptyElement(x){
    _(x).innerHTML = "";
}
function paschange(){
        var email = _("email").value;
    var pass = _("pass").value;
        var pass1 = _("pass1").value;
    var pass2 = _("pass2").value;
    if(pass == "" || pass1 == ""||pass2==""||email==""){
        _("status").innerHTML = "Fill out all of the form data";
    } 
    else if(pass1!=pass2)
    {_("status").innerHTML = "Password dont match";
}
    else {
        _("passbtn").style.display = "none";
        _("status").innerHTML = 'please wait ...';
        var ajax = ajaxObj("POST", "javat.php");
        ajax.onreadystatechange = function() {
            if(ajaxReturn(ajax) == true) {
                if(ajax.responseText == "login_failed"){
                    _("status").innerHTML = "Fields empty.";
                    _("passbtn").style.display = "block";}
                    else if(ajax.responseText == "no match"){
                        _("status").innerHTML = "password mismatched";
                    _("passbtn").style.display = "block";
                } 
                else if(ajax.responseText == "data wrong"){
                        _("status").innerHTML = "raw or pin wriong";
                    _("passbtn").style.display = "block";
                } 
                else if(ajax.responseText == "username_mismatched"){
                        _("status").innerHTML = "username didnt match with username";
                    _("passbtn").style.display = "block";
                } 
                else if(ajax.responseText == "success"){
                        _("status").innerHTML = "gotcha";
                } 
                else {
                        _("status").innerHTML = "not now";
                }
            }
        }
        ajax.send("email="+email+"&pass="+pass+"&pass1="+pass1);
    }
}
</script>
</head>

<body>

           <form id="passchanger" onsubmit="return false;">
           <input id="email" type="email" placeholder="Login Email for verification" /> 
           <input id="pass" type="password" placeholder="Old password" /> 
           <input id="pass1" type="password" placeholder="New password" /> 
           <input id="pass2" type="password" placeholder="Confirm password" />
           <button id="passbtn" onclick="paschange()">Log In</button> 
           <p id="status"></p>                      
            </form>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

我在这里缺少一件事: 在PHP代码的开头,有一行

if(isset($_GET["u"])){

要求一个名为'u'的GET参数 在你的javascript代码中,我没有看到任何对该参数传递给ajax-request的引用(我可能会错过它,很抱歉,如果是这种情况)

如果该参数未传递给ajax-request,则该行与html启动前的结束'}'之间的代码永远不会被执行,因此没有响应(只有html,因为一行有''它上面的exit()'也永远不会执行)

总的来说,你的代码对我来说很好看