tags:

views:

350

answers:

3

After submitting a form with post method f5 will resubmit that. What's the best to avoid this without redirecting a page. Idon't like to disturb the user like this. Stackoverflow are immune to f5 but i don't see any redirection after asking a question.

+6  A: 

Get After Post

  • Form does a POST request
  • Code processes form
  • Code redirects using Location header

Result: refreshing the resulting page will merely display it again, since it was done using GET.

Jani Hartikainen
+3  A: 

The standard approach for achieving this effect is to use an HTTP redirect, which isn't obvious to the user (so I assume that you refer to a meta refresh delayed redirect).

See the Post Redirect Get pattern.

David Dorward
+4  A: 

StackOverflow is pretty AJAX-heavy, which is why you're seeing the behavior you see.

If you don't want to get all AJAXy, you want redirects. Redirects of this sort should be transparent to the user:

if (! empty($_POST)){
   // Do something with the contents of $_POST
   header('Location: success.php');
}

Now, if your validation fails, you probably reload the form with some error messages, and hammering F5 will re-POST the data. But if the operation is successful, the user will be redirected to your success page, and they can hammer f5 all day without rePOSTing data, and potentially creating duplicate actions.

timdev