Repeating forms is one of the most common and troublesome problems in multi-user Web applications. There are many scenarios where you will encounter duplicate commit issues, such as:
- Click the Submit button two times.
- Click the Refresh button.
- Use the browser Back button to repeat the previous action, causing the form to be submitted repeatedly.
- Repeat the form by using browser history.
- The browser repeats the HTTP request.
Several ways to prevent forms from repeating submissions
Disable the Submit button. use JavaScript to make the Submit button after the form is submitted disable
. This method prevents impatient users from clicking the button multiple times. There is a problem, however, that if the client disables JavaScript, this method will not work.
My previous article has said that using some jquery plugins works well.
post/redirect/get mode. perform page redirection after commit, which is called Post-redirect-get (PRG) mode. In short, when the user submits the form, you go to perform a client redirect and go to the Submit Success Information page.
This avoids repeated commits that are caused by the user's F5, and it does not appear as a warning of repeated submissions by the browser form, but also eliminates the same problems caused by browser forward and backward.
Store a special symbol in the session. when the form page is requested, a special character string is generated, in the session, and in the hidden field of the form. When you accept processing form data, check that the identity string exists and immediately remove it from the session and process the data normally.
If there is no valid flag string found in the form submission, this indicates that the form has been submitted, ignoring the submission.
This gives your web app a higher level of XSRF protection.
add constraints to the database. Add a unique constraint to the database or create a unique index to prevent duplicate data from appearing. This is the most effective way to prevent data from being repeatedly submitted.
Form Repeat submission Workaround