简体   繁体   English

如何通过Ajax完成用户名和传递请求时重定向页面

[英]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. 我试图使登录系统将通过php和ajax来检查用户名和密码。我正在展示我的所作所为。

Html 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 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 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. 一切正常,但现在我只需要在用户输入正确的用户名和密码时才需要重定向。我正在使用标头定位功能,但实际上我是通过ajax发送表单值的,这就是为什么我的重定向无法正常工作。我不知道为什么请帮忙。

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. 它将告诉浏览器从新URL获取Ajax响应的数据。

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. 如果要将用户发送到新页面 ,则必须将一些数据返回到浏览器,进行检测,然后使用JavaScript将location设置为新值。

Can you do the redirect in javascript? 您可以使用JavaScript进行重定向吗?

Adding the done and fail methods to the jQuery ajax call, (assuming version > 1.8) 将完成和失败方法添加到jQuery ajax调用中(假定版本> 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>"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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