views:

174

answers:

3

Is it possible to to submit an HTML Form (with a PHP action) and include a javascript onsubmit="VerifyFields()?

I am trying to do it now, but it seems that it will do the PHP action and not do the onsubmit.

Update:

Thanks for all of your help.

The problem was that I was putting my VerifyMe() function in the <head></head>, as opposed to the <body></body>.

+5  A: 

If return false in the onsubmit handler, the form will not submit, if you return true, it will.

You should make the function VerifyFields return true or false, and call it like this:

onsubmit="return VerifyFields();"
BC
@o.k.w this will always return false so you can never submit .. regardless of the verification result..
Gaby
@Gaby, my bad, what was I thinking? Btw, deleted my comment.
o.k.w
+1  A: 
<script type="text/javascript">
   function validateForm(){
   ...
   }
</script>

<form name="contact" action="<?=$_SERVER['PHP_SELF']?>" 
    method="post" onsubmit="return validateForm();">
        <label> Foo </label>
        <input type="text" class="txt_input" name="sender_name" />
        ...
        <input type="image" src="img/send.jpg" id="submitButton" 
           name="submitForm" value="send" />
</form>
mre
A: 

I would test the inputs bevor submitting any fields to the server. I would test it client side by javascript and finaly send it to the server. You could call Your script also this way.

<input type="text" class="txt_input" name="sender_name"  onchange="validateForm();" />
streetparade
So an alternative to using on submit, jus' triggering off of the final field. Doesn't totally degrade gracefully (e.g. if you put a cursor in a different field and hit enter, the form will still submit still without any validation. If you were to change that to `onchange="validateField();"` then it might make more sense.
Tchalvak