简体   繁体   中英

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.

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.

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

当ajax授权成功时,在javascript成功回调中使用window.location="<URL>"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM