470,594 Members | 1,370 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,594 developers. It's quick & easy.

How to stop form submit when onClick event returns false


Folks

I have the following code for my submit button

<input type="submit" name="action" value="Save Article"
onClick="this.value='Saving...'; performPrePostChecks();">

I have tested performPrePostChecks() - Its a long script, but I have proven
its success by having an alert box echo the success with a simple true or
false message like so:

<script type="text/javascript">
function performPrePostChecks()
{
if(checkRequiredFormFields('form1','headline','Hea dline')==false)
{ alert("false");
return false;
}

alert("true");
return true;
}
</script>
How come that irrespective of the function performPrePostChecks returning
true, or false, that the form is still submitted?

Can someone point me in the right direction?

Thanks,
Randell D.
Jul 20 '05 #1
4 127960
"Randell D." <re**********************@and.share.com> wrote in
news:g%Wwb.497470$pl3.300911@pd7tw3no:
I have the following code for my submit button

<input type="submit" name="action" value="Save Article"
onClick="this.value='Saving...'; performPrePostChecks();">
[snip]
How come that irrespective of the function performPrePostChecks
returning true, or false, that the form is still submitted?


Because that's what you asked it to do. To be more specific, an action
handler is treated as if it's a function, with the return value determining
whether or not to go ahead. You don't return anything, so the default is
"yes, go ahead." Try returning the result of your checking function.

Is it possible that you're used to Perl, where a function (aka sub) by
default returns the value of the last statement executed? Javascript
doesn't use that convention; if you want to return a value, you have to do
so explicitly.
Jul 20 '05 #2
RR
> onClick="this.value='Saving...'; performPrePostChecks();">

To clarify Eric's correct comments, you want:
onClick="this.value='Saving...'; return performPrePostChecks();"

regards,
RR
Jul 20 '05 #3
put the funtion that returns fale in inte forms onsubmit handler intead of
the submit button.

Stu

"Randell D." <re**********************@and.share.com> wrote in message
news:g%Wwb.497470$pl3.300911@pd7tw3no...

Folks

I have the following code for my submit button

<input type="submit" name="action" value="Save Article"
onClick="this.value='Saving...'; performPrePostChecks();">

I have tested performPrePostChecks() - Its a long script, but I have proven its success by having an alert box echo the success with a simple true or
false message like so:

<script type="text/javascript">
function performPrePostChecks()
{
if(checkRequiredFormFields('form1','headline','Hea dline')==false)
{ alert("false");
return false;
}

alert("true");
return true;
}
</script>
How come that irrespective of the function performPrePostChecks returning
true, or false, that the form is still submitted?

Can someone point me in the right direction?

Thanks,
Randell D.

Jul 20 '05 #4

"RR" <ne****************@spamgourmet.com> wrote in message
news:Ke*****************@news-server.bigpond.net.au...
onClick="this.value='Saving...'; performPrePostChecks();">


To clarify Eric's correct comments, you want:
onClick="this.value='Saving...'; return performPrePostChecks();"

regards,
RR


Luvely Juvely... thanks to both of you - and the nice thing is, I actually
understand where I went wrong and why it should be the way you guys
suggested...

Thanks for the help,
randell d.
Jul 20 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

17 posts views Thread by Mike Gratee | last post: by
1 post views Thread by Fabio Cavassini | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.