How to stop form submit when onClick event returns false

R

Randell D.

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','Headline')==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.
 
E

Eric Bohlman

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.
 
R

RR

onClick="this.value='Saving...'; performPrePostChecks();">

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

regards,
RR
 
S

Stuart Palmer

put the funtion that returns fale in inte forms onsubmit handler intead of
the submit button.

Stu
 
R

Randell D.

RR said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top