Reputation: 325
I have this submit button on my website.
<form name="bbsform" action="<?=$PHP_SELF?>" method="post"
<input type="submit" value="Submit" onclick="this.disabled=true,this.form.submit();" />
</form>
The onClick part disables the submit button immediately after the first click, to prevent possible double submission of the form. And it perfectly works.
And I also have made this javascript function, too. Let's say it's called 'Guidance(bbsform)'.
Briefly, when a user forgot to complete one field in the form, it shows an alert message and focuses to the field. (It work like that some invisible cursor is automatically clicking on the field, so the user can start typing right away.) This function works great too.
They both work great, well at least separately.
But when I combine them like this :
onclick="return Guidance(bbsform);this.disabled=true,this.form.submit();"
Only the Guidance function properly works.
Could you tell me what's wrong with the code?
Thank you for reading. I'm a newbie to this whole programming thing, so please be generous :)
Here is the Guidance function. Sorry none of the solutions worked.. It's really frustrating.
function Guidance(frm){
if(frm.subject.value == ""){
alert("Please complete the title field.");
frm.subject.focus();
return false; }
try{ content.outputBodyHTML(); } catch(e){ }
if(frm.content.value == ""){
alert("Please complete the message field.");
frm.content.focus(); return false; }
Upvotes: 0
Views: 4080
Reputation: 38757
Solution by OP.
I couldn't find the right way to solve the problem directly, so I've found a workaround :)
I have found another function, and discarded 'onclick="this.disabled=true,this.form.submit();"'
The new JavaScript function I found is this - function SubmitOnce()
var submitted = 0;
function submitOnce() {
if(!submitted) {
submitted ++;
return true;
} else {
return false; }}
And I called the new function directly to the form onSubmit.
<form name="bbsform" action="<?=$PHP_SELF?>" method="post" enctype="multipart/form-data" onsubmit="return submitOnce()">
And later in the submit button code, I put in the Guidance function onClick.
<input type="submit" value="Submit" onclick="return Guidance(bbsform);" />
This works perfect! No double submission no matter what users do. (repeatedly hitting the Enter key, double - triple clicking, all of them submits only once!)
Upvotes: 0
Reputation: 8337
onclick="if (Guidance(bbsform)==true) {this.disabled=true;this.form.submit();} else {return false;}"
Upvotes: 0
Reputation: 5897
remove the 'return' in your onclick it's not getting to the second piece.
also you should look into unobtrusive javascript as a solution, then you could use an anonymous function to execute those two.
Upvotes: 0
Reputation: 2780
Well you will have to also post the Guidance function, but i guess all you have to do is return the guidance function with a true:
function Guidance(something){
//do something
return true;
}
Upvotes: 1
Reputation: 5920
Put the two JS into a single function, and then call that function onClick.
Upvotes: 0