Affan Ahmad
Affan Ahmad

Reputation: 451

How can i redirect page when username and pass request complete via ajax

I am try to make login system this will check user name and password via php and ajax.i am showing what i have done.

Html

  <div class="container">
  <div id="show-error" style="display:none;" class="alert alert-warning"></div>
  <form  class="form-signin">
    <h2 class="form-signin-heading">Please sign in</h2>
    <input type="text" id="user" class="form-control" placeholder="Email address"  required autofocus>
    <input type="password" id="pass" class="form-control" placeholder="Password"  required>
    <a class="btn btn-lg btn-primary btn-block" onclick="submitfom();" >Sign in</a>

  </form>

JavaScript

function submitfom() {
$.ajax({
          type: "POST",
          url: "checking.php",
          data: {user: $("#user").val(),pass: $("#pass").val()},
       success: function(data){$('#show-error').css("display","inline");$('#show-error').html(data);},
       });
}

PHP

session_start();
include('../../../config.php');
     if((isset($_POST['user'])) && (isset($_POST['pass'])) ){
     $user = $_POST['user'];
     $pass = $_POST['pass'];
     $query = mysql_query("SELECT user FROM admin_user WHERE user ='".$user."' ");
     $query2 = mysql_query("SELECT pass FROM admin_user WHERE pass ='".$pass."' ");

       $result= mysql_num_rows($query);
       $result2= mysql_num_rows($query2);

        if((!empty($result)) && (!empty($result2))){
        $_SESSION['admin']=$user;
      echo "Welcome ". $_SESSION['admin'];
       header('Location: ../../index.php');
        }

        else {echo "Please Write correct <strong>username and password</strong>";}
     }
       else "Some thing miss";

Every thing work fine but now i just need when user write correct username and password than i just need redirection.I am using header location function but actually i am send form values via ajax that's why my redirection not working.i don't know why please help.

Upvotes: 0

Views: 296

Answers (3)

Daniel Billingham
Daniel Billingham

Reputation: 1411

Can you do the redirect in javascript?

Adding the done and fail methods to the jQuery ajax call, (assuming version > 1.8)

done: function(data){
   window.location.replace("../../index.php");
},
fail: function(){
   $('#show-error').css("display","inline");
   $('#show-error').html(data);
}

Upvotes: 2

Noampz
Noampz

Reputation: 1205

When the ajax authorization success use window.location="<URL>" in the javascript success callback.

Upvotes: 1

Quentin
Quentin

Reputation: 944076

Using a location header to redirect will "work". It will tell the browser to get the data for the Ajax response from the new URL.

If you want to send the user to a new page, then you have to return some data to the browser, detect it, and then set location to a new value with JavaScript.

Upvotes: 2

Related Questions