File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes cannot get javascript to prevent duplicated form submit Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "cannot get javascript to prevent duplicated form submit" Watch "cannot get javascript to prevent duplicated form submit" New topic
Author

cannot get javascript to prevent duplicated form submit

Sigrid Kajdan
Ranch Hand

Joined: Jan 14, 2007
Posts: 72
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
Sushma Sharma
Ranch Hand

Joined: Jun 02, 2005
Posts: 139
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

Joined: Jan 14, 2007
Posts: 72
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

Joined: Jan 14, 2007
Posts: 72
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

Joined: Jun 02, 2005
Posts: 139
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

Joined: Jan 14, 2007
Posts: 72
Hi Sushma,

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

Sigrid
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: cannot get javascript to prevent duplicated form submit