AmitG
AmitG

Reputation: 10543

How to tackle double submit form?

How to handle a situation where user might accidentally click submit button more than one time? If it happens then two rows will be generated in table. How to avoid it. I am working in JSP.

Also want to avoid back button and refresh page which cause to create a problem of double entry in database.

Upvotes: 1

Views: 2092

Answers (7)

Kartoch
Kartoch

Reputation: 7779

Several way:

  1. You can use javascript to forbid two cliks in the submit button, but it does not work if the user use the "back" button of the navigator between the two clicks.

  2. On the server side, you can send the form with an unique id kept in the user session (could also be used for CSRF counter-measure). Then when submitting the form you can compare then remove the id in the user session. Be carreful for concurrent access to the session (each request has its own thread).

  3. Detect in the database, but it depends of the data and the constraints on it. One extension of the point 2 would be to keep the generated ID of the form in the database and removing it during the first access. This method has the advantage to user the transaction support in case of concurrent access.

  4. If the form update an specific entry in the database (row id is sent with the form), the problem could be considered as solved by itself, as it update twice the same row with the same data.

Upvotes: 2

0xCAFEBABE
0xCAFEBABE

Reputation: 5666

I'd suggest never to rely on anything client-side too hard. Using very accessible tools you can pretty much change any behavior that is client-side enforced. If your application crashes/has undefined behavior after something client-side fails, you should change your application.

One thing you could do is to make a class that takes all the fields resulting from this POST, override it's equals() method to honor all the fields, hold the request at the user level and compare all other incoming requests to the list of requests you already have for that user.

You can then remove the request objects as soon as they have been successfully processed.

If you, on the other hand, somehow get the same request again by the user (by looking through the list of 'open' requests whenever the user POSTs a request), you can just ignore it.

Upvotes: 2

David
David

Reputation: 20063

If you want to make 100% certain that the submit button cannot be clicked twice, you could disable it in the javascript within the onclick= method of the button.

For instance, in the javascript (jquery) below, if the button id was 'send'...

$('#send').click(function(e) {
    this.disabled = true;
    businessLogicCall();
});

Upvotes: 1

ManMohan Vyas
ManMohan Vyas

Reputation: 4062

First solution . Disable sumbit button .. second check some unique key if possible ...

Upvotes: 1

Alexei Kaigorodov
Alexei Kaigorodov

Reputation: 13515

Disable the submit button until user changes the input information, thus showing he wants to submit another portion of information.

Upvotes: 0

codeMan
codeMan

Reputation: 5758

First, on the client side, disable the button as soon as the user once clicks on it.

Second, on the server side, Some information in that form for will be a primary key in your database table. So when the same information is sent again to the server while inserting the data, you will get an exception since there cannot be duplicate values of primary key.

Upvotes: 1

m0skit0
m0skit0

Reputation: 25863

Have a wait time for which you don't accept any more clicks on the button, for example disabling the submit button.

Upvotes: 0

Related Questions