46

I have a form submission via POST. I submit the form, and all is well, however if I try to reload the new page that the form goes to after submission, I get the "Do you want to resend data" message (Firefox). It might happen in other browsers too, but I'm not sure.

Is there a way to stop this message popping up so I can go ahead and refresh the page? It's not good for production environments - users may submit the same form twice!

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bojangles
  • 99,427
  • 50
  • 170
  • 208
  • May be I misunderstood the question . Are you asking how to develop such a form ? – Madhur Ahuja Dec 01 '10 at 17:43
  • You do not need the `post/redirect/get` pattern anymore. See [this answer](https://stackoverflow.com/a/47247434/4632019) – Eugen Konkov Nov 12 '17 at 11:29
  • Possible duplicate of [How to prevent form resubmission when page is refreshed (F5 / CTRL+R)](https://stackoverflow.com/questions/6320113/how-to-prevent-form-resubmission-when-page-is-refreshed-f5-ctrlr) – Eugen Konkov Nov 12 '17 at 11:31

4 Answers4

51

You need to use the the POST-Redirect-GET pattern.

Make your form respond with a redirect to a GET request.
This way, when the user refreshes the page, it will only resend the GET.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • For example, OrderSubmit comes back with a 403 redirecto to OrderConfirmationView. That when a user hits refresh (or sends the link to a friend, or bookmarks it, etc.), they only refresh the view not the action. – Michael Kopinsky Dec 01 '10 at 17:44
13

An easy way after response.sendRedirect is to reload the page in this way:

window.location.href = window.location.pathname;

It works for me.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Merboy
  • 147
  • 1
  • 2
  • Could you please elaborate and give more details? – 3xCh1_23 Feb 24 '16 at 20:52
  • 1
    @Damir: rather than a "reload", this is like going to a new page, except it's the same page. As if you clicked on a link that will send you to the same page you're already on. – Anthony Mar 28 '17 at 07:47
-1

If the URL does not have any parameter, use this -

window.location = window.location.href + "?rnd=" + Math.random();

Or else use this -

window.location = window.location.href + "&rnd=" + Math.random();

It will work.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ashwin
  • 12,081
  • 22
  • 83
  • 117
  • 7
    This is a very ugly solution. Adding random numbers to the end of a POST request is probably the worst way of solving the original problem – Bojangles Oct 21 '14 at 07:05
  • 1
    @Bojangles - Can you clarify the drawbacks? – Ashwin Oct 21 '14 at 08:32
  • 2
    @Bojangles: Even JQuery uses this technique (they use it within their "cache"-Option within $.ajax()-Calls, to prevent that the Ajax-Target-URL is cached). So I don't think that it is the "worst" thing to do. It works and it works as expected. So I think it's a good and easy solution for the problem! – Manny_user3297459 Dec 29 '15 at 12:03
  • 1
    Why clutter up the URI when you don't have to? There are better solutions above. – RationalRabbit Apr 11 '18 at 15:23
-3

It will be a security violation if browsers do that. For example, in credit card processing, the site may charge the user twice.

The only way round is go to the address bar and hit Enter. It will cause a new request.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Madhur Ahuja
  • 22,211
  • 14
  • 71
  • 124