Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 发布数据并刷新页面_Php_Html_Http Post - Fatal编程技术网

Php 发布数据并刷新页面

Php 发布数据并刷新页面,php,html,http-post,Php,Html,Http Post,我有一个编辑表单页面来编辑我的网站帖子。 它使用post方法对同一页进行发布。 如果表单编译正确,将显示一条祝贺消息 问题: 当用户点击刷新按钮时,脚本会尝试将数据重新发布到页面。 有没有办法避免这种情况 谢谢 Luca您需要使用该模式。这称为模式。您可以通过使用302/303重定向响应POST请求来实现这一点,这可以防止客户端出现这种麻烦的行为 您可以在我上面发布的链接中了解更多信息。PRG模式的总体轮廓如下: if ( $_SERVER['REQUEST_METHOD'] == 'POST'

我有一个编辑表单页面来编辑我的网站帖子。 它使用post方法对同一页进行发布。 如果表单编译正确,将显示一条祝贺消息

问题:

当用户点击刷新按钮时,脚本会尝试将数据重新发布到页面。 有没有办法避免这种情况

谢谢

Luca

您需要使用该模式。

这称为模式。您可以通过使用302/303重定向响应POST请求来实现这一点,这可以防止客户端出现这种麻烦的行为


您可以在我上面发布的链接中了解更多信息。

PRG模式的总体轮廓如下:

if ( $_SERVER['REQUEST_METHOD'] == 'POST' )
{
     /// do your magic

     $_SESSION['error'] = "Thanks for your message!";

     // this should be the full URL per spec, but "/yourscript.php" will work
     $myurl = ...;

     header("Location: $myurl");
     header("HTTP/1.1 303 See Other");
     die("redirecting");
}

if ( isset($_SESSION['error']) )
{
     print "The result of your submission: ".$_SESSION['error'];
     unset($_SESSION['error']);
}

您应该使用上面已经提到的PRG模式! 为了完整起见,如果您的表单依赖于js,我添加了使用javascript history.replaceState的可能性(例如,noscript应使表单或类似内容无效…)


window.history.replaceState({},#no reload');
<script>
  window.history.replaceState({}, '#no-reload');
</script>