Handle form resubmission
- What is form resubmission?
When user performs any action like insert, update, delete or opens pop up window from website page and then hits the browser refresh button or CTRL+F5, at that time there is one confirm box asking for permission to do repeat action (form resubmission on Chrome). If user clicks on “Ok”/”Continue” then the same action will be performed again which was done by user before the refresh.
- What are the disadvantages of form resubmission?
- Most important disadvantage is, due to form resubmission duplicate entries can be inserted into the database if application has not any constrain on duplicate entries.
- Same block of code will execute again and again on each refresh after any action.
- How to prevent unwanted changes due to form resubmission?
1) Add constrains in database to prevent repeat entries
- Simply add primary key or unique keys to columns
- In some cases developer cannot add constrain for duplicate entries because of requirements. Like any documentation related entries where user can enter same amount/text again and again, so developer has to remove constrain on duplicate entries, then form resubmission can affect the application most.
- In such cases if user enter an amount/text and does the refresh or CTRL+F5 then duplicate entry will be inserted into the database and it will affect application log, calculations etc.
2) Redirect the page to another page or page itself
- This is the good solution, but if you redirect to another page it will break the flow of user. If you redirect to same page then it will again execute Page load event.
- Also it will not show your success messages because code will start executing from page load event.
- To show messages, you need to pass some variable in query string and use this variable to show success messages. You need to add following lines in both the cases if you redirect to another page or redirect to same page.
3) Add C# code to prevent to execute same code.
- This is based on time of request.
- When page receives first time request it will save the date and time in to session variable. Pre render event saves it into View state variable. When user clicks on button then it compares both the variables and gets true, then event code will be executed.
- Next, on refresh or CTRL+F5, it will not save new time into session variable. So at the comparing line will go out of the IF loop.
- Add following events and code lines
- How to test form resubmission affects in website?
- Just try with forgot password functionality. If your application has forgot password with one textbox for email address and Send password/Submit button. Then enter a valid email address and hit send password/submit button. Valid email will receive a password recovery mail. Up to this, it is a regular procedure. Now for testing form resubmission, click on refresh button of browser or hit CTRL+F5 then confirmation window for repeat action will appear and then click on Ok. You can’t recognize the change on browser but at the background same code will execute again and at the end above valid Email address will again receive password recovery mail.
- So conclusion is that, due to refresh, background code for last action will execute every time when user hits refresh or CTRL+F5.
- If your application does not have forgot password functionality or you have already added constrain for repeat action then for testing, find another click event in your website.
- Next, in user registration form, in this case same user details will be added into the database again if application has not any constrain for duplicate user.
- In multiple emails sending code, if user does the refresh or CTRL+F5 then all emails will send again to all users and it will create misunderstanding in users.
- If website contains popups then open a popup and save/close it. Then click on refresh button of browser or CTRL+F5. Same popup will open again due to repeat action of form resubmission.
*All above examples will not appear if you have redirected page on save/submit button or you have added constrain on duplicate entries in code or in database.