【问题标题】:unable to redirect the url after get data from ajax从ajax获取数据后无法重定向url
【发布时间】:2018-04-07 10:22:21
【问题描述】:

我通过 ajax 获取数据,然后验证它并根据条件重定向用户。就像用户登录成功然后重定向到仪表板,否则重定向到限制页面。我在网络控制台(开发人员工具)中获得了结果数据,但我无法重定向到特定页面。 这是我的 index.php 页面。

<div id="loginContainer">
   <form id="loginFrm" method="post">
      <h3>Members Login Area</h3>
      <img id="divider" src="images/divider.png">
      <input type="email" name="lemail" required placeholder="Enter a valid email address">
      <input name="lpassword" placeholder="Password" type="password" required>
      <input type="submit" value="Sign In">
      <input id="submit" type="hidden" name="submit">
      <p><a href="#">Forgot Password ?</a></p>
      <p id="bottom">Don't have account? <a href="#" id="signup">Sign up here</a></p>
   </form>
</div>
<script>
   $(document).ready(function () {
       $("#loginFrm").submit(function () {
           var data = $("#loginFrm").serialize();
           insertRecords(data);
           return false;
       });
       function insertRecords(data) {
           $.ajax({
               url: 'loginProcess.php',
               data: data,
               type: 'POST',
               dataType: 'html',
               success: function (data) {
                   $("#result").html(data);
                   },
                   error: function () {
                       alert('Error : COde not work properly');
                   }
               });
           }
       });
</script>

这是我的 loginProcess.php 页面,我从用户输入中获取数据。

<?php
ob_start();
error_reporting(E_ALL);
print_r($_POST);// see all result in network console
require_once('inc/dbconnection.php');
require_once('inc/functions.php');
$emailErr = $password="";
if (isset($_POST['submit'])) {

if (empty($_POST["lemail"])) {
    $emailErr = 'Please enter your email address.';
} else {
    $email = filterEmail($_POST["lemail"]);
    if ($email == FALSE) {
        $emailErr = 'Please enter a valid email address.';
    }
}
if (empty($_POST["lpassword"])) {
    $passwordErr = 'Please enter your password here.';
} else {
    $password = sha1($_POST['lpassword']);
}
}
// after pressing login, checking if the variables exist in the database
if (empty($emailErr) && empty($passwordErr)) {
    $query = $db->prepare("SELECT password FROM users WHERE email=?");
    $query->execute(array($email));
    if ($query->fetchColumn() === $password) {
        // starts the session created if login info is correct
        session_start();
        $_SESSION['email'] = $email;
        header("Location: dashboard.php",true, 302);
        exit;
    } else {
        header("Location: restrict.php",true, 302);
        exit;
    }
} else {
    header("Location: index.php?Email=$emailErr&Password=$passwordErr",true, 302);
    die;
}
ob_end_flush();
?>

注意:我在删除标题函数中的 (true, 302) 后检查过

【问题讨论】:

    标签: php jquery html ajax redirect


    【解决方案1】:

    您确定应该从您的 PHP 表单处理脚本重定向吗?我相信您应该只生成 ERROR/NO ERROR 响应,并通过处理 AJAX 响应案例从表单页面重定向页面。

    【讨论】:

      【解决方案2】:

      如果您从中获取结果的页面不包含ajax() Javscript 函数所期望格式的数据(由重定向的结果页面引起),则 AJAX 请求将失败。

      为什么不将 URL 从 PHP 传递给 Javascript 函数,检查要重定向到的 URL 是否设置然后相应地重定向?

      window.location.replace('dashboard.php');
      

      【讨论】:

        【解决方案3】:

        您不能使用AJAXphp 中重定向。您需要在AJAXsuccess 函数中重定向,如下所示:

        window.location.href = 'dashboard.php'
        

        【讨论】:

        • 我使用以下功能,但对我不起作用。 echo ("");
        • 在这样的成功函数中尝试响应 ===> 成功:function (data) { window.location.href = 'dashboard.php' },
        • 如果条件不成立,那么如何重定向到限制页面?
        • 您只需要在您的 js 中添加 if else 条件。就像如果条件为真那么'window.location.href ='dashboard.php'或者如果条件为假那么'window.location.href ='restrict.php'
        • 看看我的错误是什么......我总是在其他部分重定向(restrict.php)成功:函数(数据){$(“#result”).html(数据); if(data.success){ window.location = 'dashboard.php'; } 其他 { window.location = 'restrict.php'; } },
        猜你喜欢
        • 1970-01-01
        • 2017-10-11
        • 2021-10-31
        • 2019-09-23
        • 2021-09-29
        • 1970-01-01
        • 2015-09-08
        • 1970-01-01
        • 2012-08-08
        相关资源
        最近更新 更多