Home » Html » Stop browsers asking to resend form data on refresh?

Stop browsers asking to resend form data on refresh?

Posted by: admin November 30, 2017 Leave a comment

Questions:

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.

Does anyone know of 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!

Answers:

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 re-send the GET.

Questions:
Answers:

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

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

It works for me.

Questions:
Answers:

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

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

Questions:
Answers:

If 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.