How can I prevent reloads when a form is submitted?
Question by Compi | 2012-09-03 at 10:15
On my homepage, there is a form, from which user inputs are to be saved. Now, it is often happening, that a user clicks on Refresh in his browser, which makes, that all the data is sent for a second time, and the user inputs arrive me twice.
Is there any way to prevent this double transmission of data or the reload of the page?
Related Topics
JavaScript: Show warning when leaving the page
Info | 0 Comments
Delphi: Event Order of a Form - From OnCreate to OnDestroy
Info | 1 Comment
JavaScript: Catch Submit of Form
Tutorial | 0 Comments
Send HTML5 Canvas as Image to Server
Tutorial | 0 Comments
How to resize Image before Upload in Browser
Tutorial | 13 Comments
PHP: Submit Form with many Checkboxes to Script comfortably
Tip | 2 Comments
Important Note
Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.
Participate
Ask your own question or write your own article on askingbox.com. That’s how it’s done.
It is not possible to prevent the user from refreshing the page, it is not possible to disable the button or the function in the browser or something like that.
MySQL Solution
What you can do, however, is to store the data not for a second time. If you are saving the user input into a MySQL table, you can first check, whether the record already exists. In the event that the data is already available, you will not save the data for the second time, otherwise, you can save it.
Solution with Sessions
Another approach uses sessions. You start a session and you save a unique ID, such as a hash of the current date and time. This unique ID, you save as a hidden field together with the form.
When receiving the data from the form, you check whether the ID coming from the form matches the ID stored in thhe session and then you store a new ID in your session.
Now there are two possibilities: in one case, the ID saved in the session is the same ID like it is coming from the form. In this case, you know, that the user is sending the form for the first time, and you can save the data. If the two values do not match, you know, that the form has been already sent and saved, because you know, that the ID has changed after the first submission. In this case, you can give the user a page with a note indicating that the data has already been sent. That's it.
2012-09-05 at 05:08