Post data and refresh page

狂风中的少年 提交于 2019-11-30 05:30:56

The general outline of the PRG pattern is this:

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']);
}

You need to use the PRG pattern.

This is called the Post/Redirect/Get pattern. You do this by responding to a POST request with a 302/303 Redirect, which prevents that troublesome behavior on the client.

You can read more about this in the link I posted above.

You should use the PRG pattern already mentioned above! Just for completeness I add the possibility of using javascript history.replaceState if your forms depend on js (e.g. noscript should invalidate the form or something similar...).

<script>
  window.history.replaceState({}, '#no-reload');
</script>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!