button- can click more than once and data is getting inserted into the database repeatedly

V

vpt

Hi,

I'm using JSP,Java and Javascript to code my web page. I am having a
form in the page. On clicking the submit button, the values are getting
inserted into the database. But the problem is, i can click the button
more than once. if i click it 5 times, the same data is getting
inserted 5 times into the data base. Can anybody tell me a solution for
this problem??

Thanking You,
Vidya
 
A

Andrew Thompson

I'm using JSP,Java and Javascript to code my web page. I am having a
form in the page. On clicking the submit button, the values are getting
inserted into the database. But the problem is, i can click the button
more than once.


Many web interfaces have messaegs in red letters that say like
"PLEASE 'SEND' ONCE ONLY - OR YOU WILL BE BILLED TWICE"
The reason the pages have this message is there is not way
to absolutely *force* the use to only click the button once.
..if i click it 5 times, the same data is getting
inserted 5 times into the data base. Can anybody tell me a solution for
this problem??

Don't click the button 5 times.
 
S

Stefan Schulz

Hi,

I'm using JSP,Java and Javascript to code my web page. I am having a
form in the page. On clicking the submit button, the values are getting
inserted into the database. But the problem is, i can click the button
more than once. if i click it 5 times, the same data is getting
inserted 5 times into the data base. Can anybody tell me a solution for
this problem??

You might query the database before the insert of there was an identical
item added in a certain space of time.

That being said, i agree with Andrew, partially. There is no easy way of
doing this, but you might track "session state", and determine if this
user has already clicked your button... but this may be overblown.
 
D

dave

I'm using JSP,Java and Javascript to code my web page. I am having a
form in the page. On clicking the submit button, the values are getting
inserted into the database. But the problem is, i can click the button
more than once. if i click it 5 times, the same data is getting
inserted 5 times into the data base. Can anybody tell me a solution for
this problem??

Well, you could clear the form after submitting. Or jump to
a new page, displaying a message 'thanks for submitting'.
 
D

dave

I'm using JSP,Java and Javascript to code my web page. I am having a
Disable the submit-button after submitting.
 
V

vidya

Thanks for ur replies Mr. Andrew Thompson. But my problem is something
else. I am not clicking the button intentionly. I will see the form,
want to submit it, and will click the button once. but the page will
remain there for some more time.. may be for 10 seconds. (i am using
jdbc connection.After this page another page will come saying that ur
details have been updated.) So, i may think that i have not clicked it
properly and will click it once again. then actually the data is
getting inserted twice. This is My problem.

is there any way to block the user from clicking the button more than
once at one shot??
or is there any way to insert the data only once no matter how many
times the user clicks the button?

Please note, I am not coming back to the page and to click button once
again.. it happens accidenly..

I dont know how to solve this problem. Please help..

Thanking you,
Vidya
 
T

Thomas Weidenfeller

vidya said:
or is there any way to insert the data only once no matter how many
times the user clicks the button?

Provide some kind of generated order number in a hidden field in the
form. If you get the same order number twice, then reject the second order.

/Thomas
 
A

Andrew Thompson

is there any way to block the user from clicking the button more than
once at one shot??
No.

or is there any way to insert the data only once no matter how many
times the user clicks the button?

See Stephan's reply. (Possibly on the server side, the
*only* place where you can ensure the necessary technology
is in place)
Please note, I am not coming back to the page and to click button once
again.. it happens accidenly..

Yes, yes. I have seen at leat 6-10 commercial web-interfaces
that have the 'CLICK ONCE' message, and have particiapted in
dozens of discussions on the c.i.w.a.s-d and c.i.w.a.h. (web
oriented) groups where people want to know how to *stop* the user
clicking a button twice.

- IT CANNOT BE DONE IN ANY RELIABLE MANNER -
Since it cannot be done reliably, it is not a solution.

There might be other strategies, such as the approach
suggested by Stephan.

<made up as I went along>
You might also send the transaction and move to a different
page (no button) with message like "RETRIEVING DATA..." that
polls the server. The server continues sending pages saying
"RETRIEVING DATA...", with a refresh of a second or so. When
it has data, it writes the data to that page but with no 'refresh'.
</made up as I went along>
 
V

vidya

Thankyou very much for ur reply, sir. I have got the idea to defeat
this problem. Thanks a lot.

regards,
Vidya
 
A

Andrew Thompson

..I have got the idea to defeat this problem.

What solution are you using?

[ I thought either Thomas or Stephan's ideas
were way better than anything I came up with! ]
 
I

Ingo R. Homann

Hi,

Andrew said:
..I have got the idea to defeat this problem.

What solution are you using?

[ I thought either Thomas or Stephan's ideas
were way better than anything I came up with! ]

I think, both of them have a great problem:

(1) The user clicks the button the first time and a the order is accepted
(2) Before the "order accepted"-page is completely sent to the client,
the user clicks the button again
(3) The server recognizes the "double order" and sends a reply to the
client that says "order rejected" ("overwriting" the "ok"-result of the
first request!)

I suppose, solving this in a *real* clean way is a bit complicated (as
you metioned in your "refresh"-posting)

Ciao,
Ingo
 
T

The alchemist

I have come across this problem before, and i suggest the two best ways
to tackle it:
1. On the button click, call a javascript which disables the button.
This is very easy. Just need to add some fuctionality in javascript
which you can call by onClick().
This functionality can be seen in many web pages too.
2. You can have an intermediate page to which the page goes, on
clicking the button. The intermediate page can have all the
functionality you want to embed, like updating the database. You can
show a message there, like processing going on or something....
As soon as the backend work is done, forward the page to next one.

Hope it helps. ;-)
 
I

Ingo R. Homann

Hi,

The said:
I have come across this problem before, and i suggest the two best ways
to tackle it:
1. On the button click, call a javascript which disables the button.
This is very easy. Just need to add some fuctionality in javascript
which you can call by onClick().
This functionality can be seen in many web pages too.

This only works, if JS is enabled.
2. You can have an intermediate page to which the page goes, on
clicking the button. The intermediate page can have all the
functionality you want to embed, like updating the database. You can
show a message there, like processing going on or something....
As soon as the backend work is done, forward the page to next one.

IMHO this also is not 100% safe: If the network is slow (modem-users
like me still exists!) it may take some while until the new
"intermediate" page is displayed.

IMHO Andrew is right: The server is the only place where you can handle
the problem 100% correctly, and it can only be done using an advanced
session-management.

Having said this, I might add that, very often, a 95% safe solution is
enough. :)

Ciao,
Ingo
 
T

Thomas Hawtin

I'm using JSP,Java and Javascript to code my web page. I am having a
form in the page. On clicking the submit button, the values are getting
inserted into the database. But the problem is, i can click the button
more than once. if i click it 5 times, the same data is getting
inserted 5 times into the data base. Can anybody tell me a solution for
this problem??

This is a standard problem with almost any web application with
persistent state.

The standard solution is to use a Synchro Token. The page with the form
should be uncacheable and contain a token. When it comes to processing
the POST, check that you haven't process the same token recently
(possibly encode the time in it as well, or track session locally). Get
yourself a copy of Core J2EE Patterns
<http://www.amazon.co.uk/exec/obidos/ASIN/0131422464/>.

There is a problem that refreshing or navigating through history to the
results page will send the POST again. Or give the surfer a highly
irritating dialog to do so. The way to get around this is to only do
processing in POST. Once you have updated your persistent state,
redirect to a page which will be read and reread with a GET. The POST
should be very fast, so the user will be straight on to downloading the
GET page.

Do not use JavaScript to disable the button. If the form submit is
canceled, or otherwise does not complete, then it should be still postable.

Another way related to the Synchro Token is to structure the interface
so that the entry is created on one page, and completed on another.

Tom Hawtin
 
T

Thomas Weidenfeller

Ingo said:
(3) The server recognizes the "double order" and sends a reply to the
client that says "order rejected" ("overwriting" the "ok"-result of the
first request!)

Why would you do this? I would never send a reject message if the order
indeed got accepted at some time. It doesn't matter for the user if the
order was accepted because of the first POST or any other POST. Once
accepted, always accepted.

Just send the "order accepted" page again. Maybe with a printout of the
order and a link "In case you want to change your order ...".

If you feel like it you can also play with the wording, e.g. using "We
have already accepted your order, if you want to change your order ..."
instead of "order accepted".

/Thomas
 
V

vidya

hi alchemist,

can u please tell me the syntax to make the button inactive when i
click on it??

ya, Im having a funtion called disp()..

<input type="submit" value="Submit" name="submit" class="text"
onClick="return disp(document.form1.submit.value)" >

I use the disp fuction for validations.

Now, Inside the disp function, i have written something like

document.form1.submit.inactive;

but its not working..

is this the correct syntax?

Thanks,
Vidya
 
U

Upchuck

Maybe you should try something a little more retroactive such as
instead of blocking them from hitting the button twice completely,
which seems rather difficult blocking it mostly and then use a script
on the server to crawl your database and delete any duplicate posts.
 
V

vidya

Hi,

Thanks for ur help Mr. Thompson.

I have got the answer.

Its really simple..

In the javascript, i intoduced a new variable "submitted" with initial
value as 0. Now, Inside the if loop,
if (submitted == 0)
change submitted = 1,
do all the feild validations,
submit the form

if(submitted ==1)
give an alert telling that its already being submitted.

Thatz it!!

I got this idea from

http://javascript.about.com/library/blvalsub4.htm

Thank You once again for ur valuable directions,
Vidya
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top