14

我的网站上有以下代码(使用 php 和 smarty)来尝试避免在按 f5 时重新提交表单:

if ($this->bln_added == false) {
    if (isset($_POST['submit'])) {
        $this->obj_site->obj_smarty->assign('title', $_POST['tas_heading']);
        $this->obj_site->obj_smarty->assign('desc', $_POST['tas_description']);
    }
} else {
    $this->obj_site->obj_smarty->assign('title', '');
    $this->obj_site->obj_smarty->assign('desc', '');
    unset($_POST);
}

bln_added 默认为 false,但一旦表单提交成功后变为 true。smarty 变量 title 和 desc 在模板中用于保留表单内容,以防出现用户错误并且他们需要更改输入的内容。

如果表单提交成功,它设置bln_added = true,所以第二段代码不仅要清除表单字段,还要清空$_POST。但是,如果我按 f5,帖子数据仍然存在。

有任何想法吗?

4

11 回答 11

41

你的方法理论上可行,但有一种更简单的方法。

成功提交表单后,执行重定向。去哪里并不重要,但它会清除 $_POST。

header('Location: http://www.example.com/form.php');

在您的情况下,听起来您想重定向到您已经在的页面。如果要显示确认消息,请将 $_GET 参数附加到 URL。

希望这可以帮助,

汤姆

于 2009-04-06T18:12:49.190 回答
10

解决方案是一种通常称为Post/Redirect/Get的模式

于 2009-04-06T18:45:33.933 回答
6

处理表单的最佳方式是使用自我提交和重定向。像这样的东西:

if (isset($_POST)) {
  // Perform your validation and whatever it is you wanted to do
  // Perform your redirect
}

// If we get here they didn't submit the form - display it to them.

使用CodeIgniter 框架

function index() {
  $this->load->library('validation');
  // Your validation rules

  if ($this->form_validation->run()) {
    // Perform your database changes via your model
    redirect('');
    return;
  }
  // The form didn't validate (or the user hasn't submitted)
  $this->load->view('yourview');
}
于 2009-04-06T18:12:25.277 回答
2

您可以将表单提交重写为 AJAX 方式。如果您需要显示 HTML 内容,请以 JSON 格式返回并使用 JavaScript(例如 jQuery)插入。

于 2014-10-17T05:32:32.530 回答
1

我通过以下方式解决了这个问题(在 php 中):

  1. 在表单中添加一个不基于 time() 的唯一标识符 (id+counter) (!!!)

  2. 发布到一个单独的文件 (postform.php),该文件检查具有该唯一标识符的会话

  3. a) 如果未找到具有唯一标识符的会话:发布到数据库并使用唯一标识符填充会话

    b) 如果找到具有唯一标识符的会话:什么都不做

  4. 在 3a/3b 重定向到带有标题的结果页面之后('位置: http: //mydomain.com/mypage ')

结果是:
刷新/后退按钮没有重新提交操作,只有双击后退按钮重新提交警告(但没有重新提交操作)

于 2011-12-19T10:24:25.417 回答
1

成功发布操作后使用标题位置

header('Location: '.$_SERVER['HTTP_REFERER']);
于 2013-05-23T13:28:43.723 回答
1

如果我在代码末尾使用 header() 或 exit() 对我有用,例如,在我保存一些数据之后。

于 2015-09-13T03:09:28.130 回答
1

我发现最好的方法是使用 javascript 和 css。常见的php重定向方法 header('Location: http://www.yourdomain.com/url ); 会起作用,但它会在不同的框架和 cms(如 wordpress、drupal 等)中导致警告“警告:无法修改标头信息 - 标头已发送”。所以我的建议是遵循以下代码

echo '<style>body{display:none;}</style>'; 
echo '<script>window.location.href = "http://www.siteurl.com/mysuccesspage";</script>'; 
exit;

样式标签很重要,否则用户可能会觉得页面加载了两次。如果我们使用 style 标签 body display none 然后刷新页面,那么用户体验将和 php header('Location: ....); 一样。

我希望这个能帮上忙 :)

于 2017-06-23T12:30:11.153 回答
1

您正在寻找的答案是这个神奇的一班轮: header('Location: '.$_SERVER['HTTP_REFERER']);

例如

if(isset['submit']){
    //insert database
    header('Location: '.$_SERVER['HTTP_REFERER']);
}
于 2020-06-13T11:30:46.497 回答
0

发布后的标题重定向是必要的,但不够。

在PHP端成功提交表单后,执行重定向。例如。header('位置:http ://www.example.com/form.php ');

但这还不够。一些用户按两次链接(双击)。需要在第一次单击后禁用提交按钮的 JavaScript。

于 2011-11-27T16:44:19.750 回答
0

尝试我自己的,也许它不是最好的解决方案(快速完成),但我已经测试过它并且它有效。在 Chrome 上测试。点击查看外观BR

 <?php 
session_start(); // Start session
?>
<html>
 <head>
  <title>
    Test
  </title>
 </head>
 <body>
   <form name="test" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
   <input type="text" name="name"><br>
   <input type="submit" name="submit" value="Submit Form"><br>
  </form>

  <?php

  if(isset($_POST['submit']))
    {
     $_SESSION['name'] = $_POST['name']; //  Assign $_POST value to $_SESSION variable
      header('Location: refresh.php'); // Address of this - reloaded page - in this case similar to PHP_SELF
    } else session_destroy(); // kill session, depends on what you want to do / maybe unset() function will be enough

  if(isset($_SESSION['name']))
    {
     $name = $_SESSION['name'];

     echo "User Has submitted the form and entered this name : <b> $name </b>";
     echo "<br>You can use the following form again to enter a new name."; 
    }
  ?>
 </body>
</html>
于 2018-02-02T21:48:39.107 回答