Including JavaScript validation in the submit in PHP form

Is it possible to submit an HTML form (with PHP action) and enable it javascript onsubmit="VerifyFields()?

I'm trying to do it now, but it seems like it will execute a PHP action, not one onsubmit.

Update:

Thank you for your help.

The problem was that I put my function VerifyMe()in <head></head>, not in <body></body>.

+3
source share
3 answers

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

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

onsubmit="return VerifyFields();"
+6
source
<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>
+1

I would check the input by passing any fields to the server. I would test it on the client side of javascript and finally send it to the server. You can also call your script.

<input type="text" class="txt_input" name="sender_name"  onchange="validateForm();" />
-1
source

Source: https://habr.com/ru/post/1732445/


All Articles