One of the functionality that bugs is the dialog you get after submitting a form through a POST request. More precisely when I want to refresh the page without re-posting. Altough sometimes re-posting is the desired functionality getting rid of the functionality altogether doesn’t make sense. What if you could choose when to allow or not reposting form data.
Well recently I found a mehtod that let’s the application do just that decide if a page refresh will prompt to repost or simply do just that…refresh. The method is called POST/REDIRECT/GET or “PRG” for short.
It’s quite simple, once you’ve established that the re-posting shouldn’t occur. After a POST request, you issue a Location header REDIRECT along with a 303 response status code. Then refreshing will issue a GET instead of a POST. The Status code 303, or more precisely “HTTP/1.1 303 See Other”, means that the response to the request is located somewhere else (pointed by the Location header).
Let’s look at a simple PHP example.
<?php /* Check if post array contains data */ if ( count($_POST) > 1 ) { /* ... process form here ... */ /* prevent re-posting prompt by redirecting to same url with a 303 status */ header('Location: '. $_SERVER['PHP_SELF'] , true, 303); exit; } //if ?>
You can also enjoy a Demo
Helpful, thank you. Thought I’d leave a note because I’d seen other examples, but your wording made me ‘get’ it. No Pun intended.
Thanks