Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including JavaScript validation in a form submit in PHP

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

like image 466
littleK Avatar asked Jun 16 '26 08:06

littleK


2 Answers

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();"
like image 147
BC. Avatar answered Jun 18 '26 23:06

BC.


<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>
like image 30
mre Avatar answered Jun 18 '26 23:06

mre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!