Ajax条件不起作用

时间:2014-01-13 07:18:48

标签: php ajax

我有以下的ajax代码:

function login() {
// Create our XMLHttpRequest object
            var hr = new XMLHttpRequest();
// Create some variables we need to send to our PHP file
            var url = "login.php";
            var email = document.getElementById("email").value;
            var password = document.getElementById("password").value;
            var vars = "email=" + email + "&password=" + password;
            hr.open("POST", url, true);
// Set content type header information for sending url encoded variables in the request
            hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
// Access the onreadystatechange event for the XMLHttpRequest object
            hr.onreadystatechange = function() {
                if (hr.readyState == 4 && hr.status == 200) {
                    var return_data = hr.responseText;
                    if (return_data == "no") {
                        window.location.href = 'index.html';
                    }
                    else {
                        document.getElementById("statuslogin").innerHTML = return_data;
                    }
                }
            }
// Send the data to PHP now... and wait for response to update the status div
            hr.send(vars); // Actually execute the request
            document.getElementById("statuslogin").innerHTML = "checking...";
        }

和PHP代码

 <?php
    session_start();
    // AJAX CALLS THIS LOGIN CODE TO EXECUTE
    if(isset($_POST["email"])){
        // CONNECT TO THE DATABASE
        include_once("includes/db.php");
        // GATHER THE POSTED DATA INTO LOCAL VARIABLES AND SANITIZE
        $email = mysqli_real_escape_string($db_conx, $_POST['email']);
        $password = md5($_POST['password']);
        // GET USER IP ADDRESS
        $ip = preg_replace('#[^0-9.]#', '', getenv('REMOTE_ADDR'));
        // FORM DATA ERROR HANDLING
        if($email == "" || $password == ""){
            echo "Please enter your email id and password.";
            exit();
        } else {
        // END FORM DATA ERROR HANDLING
            $sql = "SELECT * FROM users WHERE email='$email' LIMIT 1";
            $query = mysqli_query($db_conx, $sql);
            $row = mysqli_fetch_row($query);
            $db_username = $row[1];
            $db_email = $row[2];
            $db_pass_str = $row[3];
            $db_activation = $row[4];
            if($password != $db_pass_str){
                echo "email id or password is incorrect!";
                exit();
            } 
            else if($db_activation=='0'){
            echo"Please activate your account to login! ";
            exit();
            }
            else {
            $sql1="SELECT * FROM useroption WHERE username='$db_username'";
               $query = mysqli_query($db_conx, $sql);
            $row = mysqli_fetch_row($query);
            $db_id  = $row[0];
                // CREATE THEIR SESSIONS AND COOKIES
                $_SESSION['userid'] = $db_id;
                $_SESSION['username'] = $db_username;
                $_SESSION['password'] = $db_pass_str;
                setcookie("id", $db_id, strtotime( '+30 days' ), "/", "", "", TRUE);
                setcookie("user", $db_username, strtotime( '+30 days' ), "/", "", "", TRUE);
                setcookie("pass", $db_pass_str, strtotime( '+30 days' ), "/", "", "", TRUE); 
                // UPDATE THEIR "IP" AND "LASTLOGIN" FIELDS
                $sql = "UPDATE users SET ip='$ip', lastlogin=now() WHERE username='$db_username' LIMIT 1";
                $query = mysqli_query($db_conx, $sql);


echo "no";

                }


            }
        }

    }
    ?>

当响应文本为“否”时,它应该重定向到index.html,但上面的代码不起作用 当响应文本为“否”时,它应该重定向到index.html,但上面的代码不起作用 当响应文本为“否”时,它应该重定向到index.html,但上面的代码不起作用 当响应文本为“否”时,它应该重定向到index.html,但上面的代码不起作用 当响应文本为“否”时,它应该重定向到index.html,但上面的代码不起作用 当响应文本为“否”时,它应该重定向到index.html,但上面的代码不起作用 当响应文本为“否”时,它应该重定向到index.html,但上面的代码不起作用 当响应文本为“否”时,它应重定向到index.html,但上面的代码不起作用

2 个答案:

答案 0 :(得分:1)

您需要在

之后添加exit();
echo 'no';
exit();

如果不是也返回下一句

答案 1 :(得分:0)

你能试试吗,

加入

 if($count>0){ // added condition
      ......
      echo "no";
}else{
     .....
     echo" Thank you ! One of our operator will reply you soon";
 }

PHP

    else{


            $sql1="SELECT * FROM useroption WHERE username='$db_username'";
            $query = mysqli_query($db_conx, $sql);
            $count = mysqli_num_rows($query);

            $row = mysqli_fetch_row($query);
            $db_id  = $row[0];
            // CREATE THEIR SESSIONS AND COOKIES
            $_SESSION['userid'] = $db_id;
            $_SESSION['username'] = $db_username;
            $_SESSION['password'] = $db_pass_str;
            setcookie("id", $db_id, strtotime( '+30 days' ), "/", "", "", TRUE);
            setcookie("user", $db_username, strtotime( '+30 days' ), "/", "", "", TRUE);
            setcookie("pass", $db_pass_str, strtotime( '+30 days' ), "/", "", "", TRUE);

            if($count>0){ // added condition
                // UPDATE THEIR "IP" AND "LASTLOGIN" FIELDS
                $sql = "UPDATE users SET ip='$ip', lastlogin=now() WHERE username='$db_username' LIMIT 1";
                $query = mysqli_query($db_conx, $sql);
                echo "no";
            }else{
                $sql = "INSERT INTO bookedtaxi (id,location,from_taxi, to_taxi,date, time)
                VALUES('$db_id','$location_session','$from_session','$to_session','$date_session','$time_session')";


                $query = mysqli_query($db_conx, $sql);
                echo" Thank you ! One of our operator will reply you soon";
            }

    }