• Post Reply Bookmark Topic Watch Topic
  • New Topic

People clicking Submit twice confuses database

 
Lucy Smaile
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If people get impatient and click the submit button more than once for my surveys, the data gets sent more than once to the database - resulting in some questions writing multiple duplicate answers and some throwing exceptions...
Is there any way to stop this happening? I have sometimes seen websites which explictly say "Submit. Please click only once" but I don't really want to write that. (Maybe the answer's just to have faster access...)
Thanks for any thoughts.
 
ashish kulkarni
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
There are many ways of doing it, one of them is using javascript to disable the submit button when it is clicked once, so the user will not be able to perss it again,
also u can keep a random value say "123" in session before loading this page, then print this value as hidden on HTML page, and after the user has pressed sumbit, check this hidden value with the session, if it is same submit the form, then either change this hidden value using javascript or change the session value,
so this way u can work around this problem
 
Shawn Bayern
Author
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tend to prefer server-side solutions for problems like this. For instance, you could set a session-scoped attribute to 'true' when you receive the first response. Then, simply vend the 'Thanks for your submission!'-type message if this variable is true instead of sending the results to the database.
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm doing something similar.
Before I reach the form I generate a token (random number), put it in the session and as a hidden value in the form.
When the user clicks submit I check that the hidden token value is the same as the one I have in my session. If it is, I remove the token from the session, process the form, and redirect to thanks.jps (for example).
If the user clicks more than once, the token will not be the same as the one in the sesion, so you can discard this and redirect straight to thanks.jsp.
hope it helps
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"ashishkulkarni",
The Java Ranch has thousands of visitors every week, many with surprisingly similar names. To avoid confusion we have a naming convention, described at http://www.javaranch.com/name.jsp.
We require names to have at least two words, separated by a space, and strongly recommend that you use your full real name. Please edit your profile and select a new name which meets the requirements.
Thanks.
Dave
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shawn Bayern:
I tend to prefer server-side solutions for problems like this.

Definitely. We've discussed the client-side (disable the submit button) solution before, and it's possible to lock the user out of performing any action in some cases.
The server-side token is much cleaner.
I seem to remember Struts having something like this (ie tokens) built in. Anyone seen or used them?
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Struts does implement the "Transaction Token" design pattern (I think that's what it's called). It works quite nicely. Everything is encapsulated for you and the methods are implemented by the org.apache.struts.Action class (which you subclass).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!