• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to prevent to click multi submit button?

 
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In my jsp page,I have two submit button,they will jump to different page,but when I first clicked one button then clicked the second button as quickly,my servlet cannt do with it,I want to forward to the page when the one's first click,ignore the second click.How can I do it?
Thanx alot.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you using Javascript to define 'submitXXX' functions for the click of the two buttons?
If so, you can try defining a flag as a global javascript variable.
This flag would be set when you click either of the buttons the first time, and submit would take place based on the state of the flag.
Hope this helps.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An alternative would be a piece of Javascript that runs in the onclick event of the button and disables the buttons before submitting the form.
That way they cannot be clicked again.
 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One way to do this without using javascript:
before you reach your page with the two buttons, generate a token (it can be a random number), store it in a session, and place it as a hidden value in your form.
When the user clicks the first button, get the value from the session and compare it with the hidden value. If they're the same, it means it's the first button that it was clicked. remove the token from the session.
If the user gets to click the second button, your code will check again the hidden value against the one in the session. But, since you've removed the token from the session already, the values won't be the same. You can discard this request, since you know it was the second click and not the first.
easy
[ September 09, 2003: Message edited by: Andres Gonzalez ]
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
IMHO
I think that disabling the buttons before submitting is better so that the user won't be confused if which action was really submitted.
=)
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since you can never rely on client-side Javascript executing, Andres solution is actually the more robust one.
In practice, I may frequently perform client-side actions to enhance the 'user experience' -- this include form data validation -- but my server-side code never assumes that any client-side code has executed.
bear
 
qingwu wang
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks,It's nice of you!
Because my browser cann't support javascript so much,I will take Andres' method,How can I discard request?
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by qingwu wang:
Thanks,It's nice of you!
Because my browser cann't support javascript so much,I will take Andres' method,How can I discard request?


It depends on your requirements. It's up to you on what you're gonna do once you get the unwanted request. I implemented this technique when a user was filling out his details. In this way, my data was not saved in the database twice.
If everything went ok, I forwarded the request to a thank you page. So, if the user clicked the button twice, I immediately forwarded it to thankyou.jsp, without processing anything.
hope it helps
 
qingwu wang
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
QTN:
My process as following
servlet(get serveral string)->jsp(ervery string generate one submit)->servlet(according to submit attribute jump to different page)->to servral different page.then the button in the jsp can't allow to click more than one time before jumping other page.
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can't you just do something like:

and that's it. if the second request comes in, it'll do nothing, coz A not equal to B (It doesn't go inside the if statement). You know that this request should be discarded, since you've already redirected it to your jsp.
does that make sense?
[ September 09, 2003: Message edited by: Andres Gonzalez ]
 
qingwu wang
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To:Andres

the above snippet code,when I clicked two submit button,hs.getAttribute("flag")==null ?
 
Andres Gonzalez
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
disclaimer: dirty code

In the servlet
 
qingwu wang
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Andres,thanks a lot!!!
I will try it!
 
qingwu wang
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello everyone!
I found a piece of js code as following,but I click the submit button,it seems no changes.I think it should change grey.
 
qingwu wang
Ranch Hand
Posts: 147
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to remove the form after submit on time.How can I do it?
if I delete submit button after submit?
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I think making the visibilty of the other submit button to false after u click one button would also be a good choice...
Cheers,
Gaya3
-------------------------------------------
Life is a process of continuous learning...
If you are not learning .. You are decaying...
 
What are you doing? You are supposed to be reading this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic