0

我试图在 Ajax 脚本中成功登录后刷新页面,但页面未刷新。我被迫手动刷新页面,然后才会显示已登录。在此先感谢您的帮助,非常感谢。

这是ajax的登录脚本;

$("document").ready(function() {
    $("#what").click(function() {
        var email = $("#emailLogin").val();
        var password = $("#passwordLogin").val();
        var dataString = 'email='+email+'&password='+password;
        $.ajax({
            type: "POST",
            url: "authenticate.php",
            data: dataString,
            success: function(response){                
                //alert (response);
                if (response == 0) {
                $("#problem").html("Please enter correct details");
                            } else {
                    window.location = "index.php";
                }
            }
        });
    return false;
    });
});

这是用于验证表单的 php 脚本

// authenticate.php

session_start();
require_once "database.php";
db_connect();
require_once "auth.php";


$user_id = credentials_valid($_POST['email'], $_POST['password']);

if($user_id){
  log_in($user_id);
  if($_SESSION['redirect_to']){
    header("Location: " . $_SESSION['redirect_to']);
    unset($_SESSION['redirect_to']);
  }else{
    header("Location: index.php");
  }

}else{
  echo "0";

}

?>
4

3 回答 3

0

您可以使用 javascript 重定向。AJAX 请求不能重定向用户的浏览器

if (response == 0) {
    $("#problem").html("Please enter correct details");
} else {
    window.location = "/index.php";
}
于 2012-07-25T09:25:58.313 回答
0

当然,您需要刷新页面,因为您使用 JQuery 发布,而 php 函数只返回一些数据。

您有 2 个选择:

  1. 发布经典方式(表单提交)

  2. 使用 window.location='url' 从 javascript 重定向;

于 2012-07-25T09:26:48.657 回答
0

i see you are redirecting in the js if the response is not equal to 0, and in the php you are redirecting if the log-in failed. also the server side redirecting is not working with ajax, you have to redirect it with window.location.href = 'url' also you may send the code status in the response but not writing it yourself.

于 2012-07-25T10:48:24.840 回答