【问题标题】:How do I redirect the user with PHP after using jQuery's preventDefault() on a form?在表单上使用 jQuery 的 preventDefault() 后如何使用 PHP 重定向用户?
【发布时间】:2018-12-14 15:19:44
【问题描述】:

我正在使用 jQuery、AJAX 和 PHP 来验证我网站上的大多数表单。实际的输入验证是通过 PHP 完成的(我认为这将最好防止用户使用浏览器源代码检查器来编辑脚本来绕过验证),但我使用 jQuery 和 AJAX 将错误加载到表单提交下方的错误消息 div按钮。

所有这些都可以正常工作,但是当表单成功提交后,我想调用header('Location: foo.php') 将我的用户发送回某个页面。但是,由于我使用的是preventDefault(),因此我的新页面正在加载到错误消息 div 中,这使得浏览器窗口看起来像是有两个页面重叠在一起(当前 url 也没有改变)。

有解决办法吗?我想我可能可以通过在 PHP 代码完成后包含一个脚本来取消绑定 PHP 文件中的事件,但我没有成功。

jQuery:

$(document).ready(function() {
  $("form").submit(function(event) {
    event.preventDefault();

    var url = window.location.href.toString().split("=");
    var id = url[1];

    var title = $("#title").val();
    var content = $("#content").val();
    var submit = $("#submit").val();

    //this is where the PHP is loading the new page, along with error messages
    $(".form-message").load("/php/_create.thread.php", {
      title: title,
      content: content,
      id: id,
      submit: submit
    });
  });
});

PHP 文件结束:

<?php
    //if successful, exit the script and go to a new page
    $submissionSuccessful = true;
    exit(header('Location: /index.php'));
?>

<reference path="/javascript/jquery-3.3.1.min.js"></reference>
<script type="text/javascript">

var submissionSuccessful = "<?php echo $submissionSuccessful; ?>";

if (submissionSuccessful)
{
    $("#title, #content").css(
    {
        "border": "2px solid #24367e"
    }
        );

    $("#title, #content").val("");
}

</script>

【问题讨论】:

  • 我不认为这会按照你想要的方式工作。当您使用 jQuery 处理表单提交时,您基本上会停止任何可能的浏览器级重定向。相反,您需要使用 JS 重定向(如果成功),可能使用类似:location.href = '/index.php'(抱歉,我提交评论太早了)
  • 你这是什么意思?我已经能够使用 Javascript 解决方法重定向用户,最糟糕的是,在禁用 JS 的情况下,用户可以在成功提交表单后单击按钮返回主页。我只想知道如何使用 PHP 来解决这个问题,或者我是否完全错误地解决了这个问题。这是我使用我的方法遇到的唯一问题。我的解决方法是:exit('&lt;script type="text/javascript"&gt;location.assign("/index.php")&lt;/script&gt;');
  • 我推荐一种稍微不同的方法:而不是 $(x).load(url, data) 使用 $.post(url, data, callback),url 应该返回一些像 {error:"message"}{success:true} 这样的 json(它也可能是 HTTP 错误代码!)然后,回到 JS,你可以对响应做一个条件:if (response.error) { showError } else { location.href = '/index.php' }
  • 正如@mxcoder 所说,在进行 Ajax 调用时,您无法使用 PHP 重定向用户。如果这样做,请求将跟随该重定向并从它返回响应(这是您重定向到的页面)。您需要在回调中使用 location.href 来完成此操作。通过 ajax 加载的 JS 代码不会被触发。如果 ajax 请求返回错误,则显示它们,如果返回成功,则使用 location.href 重定向用户。
  • 我也不太明白标题与您的问题或疑问有什么关系?

标签: javascript php jquery ajax


【解决方案1】:

我讲的方法和这个差不多

$(document).ready(function () {
    $("form").submit(function(event) {
        event.preventDefault();
        var url = window.location.href.toString().split("=");
        var id = url[1];
        var title = $("#title").val();
        var content = $("#content").val();
        var submit = $("#submit").val();
        // AJAX POST request to PHP
        $.post("/php/_create.thread.php", {
            title: title,
            content: content,
            id: id,
            submit: submit
        }).done(function (response) {
            // response is a JSON document
            if (response.error) {
                // Here you basically modify the UI to show errors
                $(".form-message").text(response.error)
            } else {
                // Here you basically modify the UI to show success
                $("#title, #content").css({ "border": "2px solid #24367e" });
                $("#title, #content").val("");
                location.href = '/index.php' // REDIRECT!
            }
        });
    });
});

在服务器端

<?php
if ($someSuccessCondition) {
    $response = ['success' => true];
} else {
    $response = ['error' => 'The Error Message'];
}
echo json_encode($response);
exit();

【讨论】:

  • 您可以将window.location.href.toString().split("=");(返回一个包含位置的数组)替换为window.location.href(它将返回一个包含位置的字符串)
  • 旁注:如果呼叫成功返回,您也只需要拥有location.href。由于无论如何我们都会将用户重定向到该页面之外,因此无需设置任何 css 或清空任何输入。
  • 是的,你是对的。但是在瞬间他们可以看到提交的表单,我希望他们直观地知道他们做得很好。
  • @Dalsia - 当它重定向时他们不知道吗?这就是他们应该得到一些反馈的地方。大多数用户可能不会看到这些更改。他们可能在重定向之前看到一些闪烁的东西,但不知道它是什么。
  • 是的,但是看到您的输入在被发送到下一页之前被清除是一件令人愉快的事情(在我看来)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-30
相关资源
最近更新 更多