php 在浏览器中重新加载/刷新页面而不重新提交表单?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8000798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:45:34  来源:igfitidea点击:

Reload/refresh page in browser without re-submitting form?

phpformsrefreshreload

提问by Paul

I have a form on a php page that is submitted to the same page. I noticed that if I reload/refresh the page the form gets re-submitted. How do I code to avoid this in the most easy way?

我在提交到同一页面的 php 页面上有一个表单。我注意到如果我重新加载/刷新页面,表单会重新提交。我如何编码以最简单的方式避免这种情况?

采纳答案by martinstoeckli

One possibility is, to implement the post-redirect-get approach.

一种可能性是,实施重定向后获取方法。

Simply said, a POST request will be never delivered to the browser. Instead you execute all necessary actions and store the information you need in the session, and then you make a redirect with code 303.

简单地说,POST 请求永远不会传递到浏览器。相反,您执行所有必要的操作并在会话中存储您需要的信息,然后使用代码 303 进行重定向。

$page = 'show_result.php';
header('Location: '.$page, true, 303);
exit;

Doing it this way, the browser will show the "show_result.php" page (a GET request) instead of the page requested with POST. This is also the page that is added to the history, so refreshing and using the back button will never do another POST request. As a nice side effect you get rid of browser warnings about resending data, normally the user cannot decide what to do then anyway.

这样做,浏览器将显示“show_result.php”页面(一个 GET 请求)而不是用 POST 请求的页面。这也是添加到历史记录的页面,因此刷新和使用后退按钮将永远不会执行另一个 POST 请求。作为一个很好的副作用,您可以摆脱有关重新发送数据的浏览器警告,通常用户无论如何都无法决定要做什么。

I think the biggest problem with this approach is, that you need a session to store error messages, that means you have to rely on cookies. If you do no redirect to display errors, the browser will show the warning about resending data.

我认为这种方法的最大问题是,您需要一个会话来存储错误消息,这意味着您必须依赖 cookie。如果不重定向显示错误,浏览器将显示有关重新发送数据的警告。

回答by Igor Parra

This assume a lot of things, but maybe is what you are looking for:

这假设了很多事情,但也许是您正在寻找的:

if ($_POST)
{
    $success = false;

    /*
     * if all goes OK managing POST data make $success = true;
     * 
     */

    if ($success)
    {
        // this will redirects to your original
        // form's page but using GET method
        // so re-submitting will be no possible
        header("location: {$_SERVER['PHP_SELF']}");
        exit;
    }
}

回答by Your Common Sense

According to HTTP standard, you ought to make browser to do a GET request after sending POST one.
Here is a sketch example to do the form handling:

根据 HTTP 标准,你应该让浏览器在发送 POST 后做一个 GET 请求。
这是一个进行表单处理的草图示例:

<?  
if ($_SERVER['REQUEST_METHOD']=='POST') {  

  $err = array();
  //performing all validations and raising corresponding errors
  if (empty($_POST['name']) $err[] = "Username field is required";  
  if (empty($_POST['text']) $err[] = "Comments field is required";  

  if (!$err) {  
    //if no errors - saving data and redirect
    header("Location: ".$_SERVER['PHP_SELF']);
    exit;
  }  else {
    // all field values should be escaped according to HTML standard
    foreach ($_POST as $key => $val) {
      $form[$key] = htmlspecialchars($val);
    }
} else {
  $form['name'] = $form['comments'] = '';  
}
include 'form.tpl.php';
?>