3

I have a simple form in a page.The form is submitting to the same page.After form submission when i click browser refresh button it asks to resend the form and when i click resend it submits the form again.

Is there any issue with my code or its normal browser behavior?

Is there any way to prevent this..

Deepak Kumar Padhy
  • 4,128
  • 6
  • 43
  • 79
  • You haven't tagged with the platform so it's hard to link to a dupllicate for your platform. But they are there – Rune FS Oct 31 '13 at 10:03
  • 1
    This [answer](http://stackoverflow.com/a/4473801/237838) may helpful for you. please have a look. $('form').preventDoubleSubmission(); – John Oct 31 '13 at 09:56

5 Answers5

4

This a normal behavior of the browser.You can prevent this,by redirecting to some other page once the current form is submitted.

The common solution to that is to send a redirect to a GET request after executing the POST request. This way the client will end up having the GET request in the browser view. Refreshing this will then only re-execute the GET request which doesn't (shouldn't) modify anything.

Premdeep Mohanty
  • 761
  • 5
  • 10
1

That is normal behaviour. To prevent use another page to submit to and redirect back to the form.

Petra
  • 565
  • 1
  • 7
  • 20
  • 2
    this should be a comment it's correct but not helpfull in the sense that it does not help OP solve the issue – Rune FS Oct 31 '13 at 09:56
0

Is normal behavior. When you submit the form, even if is to the same page, the browser makes a request. The refresh button sends the same request. Hence the multiple submissions.

And no... you can't do anything about it, except maybe ajax submitting the form.

zozo
  • 8,230
  • 19
  • 79
  • 134
  • you can easily do something about it. The pattern is called post-redirect-get or redirect after post – Rune FS Oct 31 '13 at 10:49
0

this is normal behaviour of browsers, if it detects a POST request and you refresh the page, your browser will automatically ask you if it has to send the request again. To prevent that.. well.. you could simply redirect to another page, or double check the form submissions on server side (see if there are no double)

0

This is a normal behaviour, but you can add, at the end of your treatment, a redirection to the same page without POST:

<?php
// set to true to display a waiting message "Thanks you, you will be redirected in 5 sec."
$wait_before_refresh = false;
if (isset($_POST['myButton']))
{
  // process to your checks, do whatever you want
  // ...
  if ($wait_before_refresh)
  { 
    $html_redirect = <<<HTML_REDIR
<!DOCTYPE html>
<html><head><title>Thanks you !</title>
<meta http-equiv="refresh" content="5;URL='/'" />
</head>
<body>Thanks you, you will be redirected in 5 sec.</body></html>
HTML_REDIR;
    echo $html_redirect;
  }
  else
  {
    header('Location : /');
    exit;
  }
}
?>
<form method="POST" action="/"><input type="submit" name="myButton" value="clicked" /></form>
Asenar
  • 6,732
  • 3
  • 36
  • 49