• 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

cannot get javascript to prevent duplicated form submit

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I would like to ask for help with a piece of javascript code intended to prevent double form submit which I cannot get to work with JSF.

First, I tried simply returning false from a javascript function when a user clicks twice. I have a variable requestSubmitted on every page, initially set to true, and a function which is called on form submit containing he following:



This does not work, as in the case of a second submission - despite of the "return false" - the form is submitted anyway, and I get duplicate requests.

Second, I tried disabling the button, and that in two different places. First time, in "if" part, after a form.submit(). In that case, there was no form submit at all:

if (!requestSubmitted ) {
requestSubmitted = true;
form.submit();
submitButton.disabled = true;
return true;

} else {
alert("duplicate submit");
}

Second time, in the "else" part. In that case, the form was not submitted either, i.e. not even the necessary first submit was carried out:

if (!requestSubmitted ) {
requestSubmitted = true;

form.submit();
return true;

} else {

submitButton.disabled = true;
alert("duplicate submit");
}

I suppose these problems come from JSF working with javascript internally, but I don't know what to do in this case. Does anybody have an idea or can explain what's going on?
thanks,
Sigrid
 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can you show how you are writing your commandButton?
it should be like this. Also resubmit should be false initially, not true as you wrote in your post. if it is true initially, then it would never go in the if block.


[ August 28, 2007: Message edited by: Sushma Sharma ]
 
Sigrid Kajdan
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sushma,

thanks for your reply!

In fact, I have no "onclick" in the command button, but an "onsubmit" in the <h:form> tag. (Sorry I can't post it here at the moment, as I'm at home now). I just followed the example given in websphere documentation (http://publib.boulder.ibm.com/infocenter/wchelp/v5r6m1/index.jsp?topic=/com.ibm.commerce.developer.doc/refs/rcsdblclickclientside.htm), adapting it to jsf:

<script>
var requestSubmitted = false;
function submitRequest() {
if (!requestSubmitted ) {
requestSubmitted = true;
return true;
}
return false;
}
</script>
...

<FORM method="POST" action="Logon" onSubmit="javascript:submitRequest()">
......
</FORM>


(One thing I did differently from the websphere example was I put the function in a .js file, while having an included script setting requestSubmitted to false in every jsp page. [Sorry for the mistake about true/false, I meant false of course]. But I think that's not a problem.)

Should that not work the same with onsubmit on the form compared to onclick on the button, or am I making a mistake here?

Thanks,
Sigrid
 
Sigrid Kajdan
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now that I already posted the former message, I wonder about the "return" in your code:

on click = " return formSubmit()"

As I remember (not being at work now), I have just <h:form onsubmit="formSubmit()"> in my code. Might this be a problem?
 
Sushma Sharma
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah, return definitely makes difference. I am not sure about the onsubmit of the form. but it should work with that also.
 
Sigrid Kajdan
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sushma,

great - the missing return was it! Thanks a lot,

Sigrid
 
reply
    Bookmark Topic Watch Topic
  • New Topic