nehaJ
nehaJ

Reputation: 77

Ajax Condition is not working

I am having following code of 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...";
        }

And php code

 <?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";

                }


            }
        }

    }
    ?>

when response text is "no" it should redirect to the index.html,but above code is not working when response text is "no" it should redirect to the index.html,but above code is not working when response text is "no" it should redirect to the index.html,but above code is not working when response text is "no" it should redirect to the index.html,but above code is not working when response text is "no" it should redirect to the index.html,but above code is not working when response text is "no" it should redirect to the index.html,but above code is not working when response text is "no" it should redirect to the index.html,but above code is not working when response text is "no" it should redirect to the index.html,but above code is not working

Upvotes: 0

Views: 82

Answers (2)

Krish R
Krish R

Reputation: 22721

Can you try this,

added

 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";
            }

    }

Upvotes: 0

Haim Evgi
Haim Evgi

Reputation: 125594

you need to add exit(); after

echo 'no';
exit();

if not is return also the next sentence

Upvotes: 1

Related Questions