Post data and refresh page

前端 未结 4 1797
深忆病人
深忆病人 2020-12-29 17:20

I have an edit-form page to edit my website posts. It uses post method to the same page. If the form is compiled correctly shows up a congrats message.

The p

相关标签:
4条回答
  • 2020-12-29 17:32

    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']);
    }
    
    0 讨论(0)
  • 2020-12-29 17:36

    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.

    0 讨论(0)
  • 2020-12-29 17:42

    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>
    
    0 讨论(0)
  • 2020-12-29 17:44

    You need to use the PRG pattern.

    0 讨论(0)
提交回复
热议问题