oceanier
oceanier

Reputation: 87

how redirect multiple locations depending on JSON.stringify(response)

I'm trying to create login page through ajax request but now facing problem when user enterd wrong password I want it remains on same page .And successful login it jumps to next page.

 $('#login').on('click',function(event){

     event.preventDefault();
     var username = document.getElementById("username").value;
     var password = document.getElementById("password").value;
     if(password == ''||username == '')
            {
                alert ('Please fill the fields!');
            }
            $.ajax({
        url: "api/learnapi.php",
        type: "get",
        dataType: "json",
       data: {type: "login",email:username ,pass:password},
        //type: should be same in server code, otherwise code will not run
        ContentType: "application/json",
        success: function (response) {
            alert(JSON.stringify(response));
            location.href ="app-student-dashboard.php";
        },
        error: function (err) {
            alert(JSON.stringify(err));

        }
     });

  });

but when correct or wrong password is enterd it jumps to app-student-dashboard.php how do i prevent it to do so. Here is my code for validating user

    $loname = $_GET['email'];
    $lopass = $_GET['pass'];

    $query1="select * from signup where email='$loname'";
    $result1= mysqli_query($conn,$query1);
    $row = mysqli_fetch_array($result1, MYSQLI_ASSOC);
 //  printf ("%s \n", $row["password"]);
    $pass1=$row["password"];


    if( $pass1 == $lopass) {
       $res["flag"] = true;
        $rest["message"] = "Login successful."; 
      }
    if($pass1 != $lopass)
    {

     $res["flag"] = false;
        $rest["message"] = "Wrong password entered."; 

    }

Is this possible to call error: function (err) from where it sends message that 'wrong password is entered'.

Upvotes: 0

Views: 49

Answers (2)

oceanier
oceanier

Reputation: 87

after getting help from @dadan ,I just put condition inside success as

success: function (response) {

              alert(JSON.stringify(response));
                    if(response.flag == true){
                             location.href ="app-student-dashboard.php";
                                  }
        },

got desired result.

Upvotes: 1

dadan
dadan

Reputation: 124

i think you should move the checking inside the success and make conditions if the response==true then redirect page..

Upvotes: 0

Related Questions