最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Including JavaScript validation in a form submit in PHP - Stack Overflow

programmeradmin1浏览0评论

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

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

Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked Feb 11, 2010 at 23:37 littleKlittleK 20.2k32 gold badges132 silver badges191 bronze badges 1
  • 1 Should be. Can you post some code? – Jeff B Commented Feb 11, 2010 at 23:39
Add a ment  | 

3 Answers 3

Reset to default 6

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();"
<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>

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();" />
发布评论

评论列表(0)

  1. 暂无评论