Home >Backend Development >PHP Tutorial >Validation method for multiple submission forms
Sometimes we need to submit a form from one page to a different page. The processing method is generally to dynamically give the action value in the onClick event, as in the following example:
94d6dd44a44e7c0bf656b9e3f20b3e02
417091cb7805976559e397fa142e1da7
3cd015235340caa036634e515c3741f5
3b675b543bf0be81309e8438b6839b60
67c47c2f4e2e39e829bbcfb43fc3cadb
At this time, the verification of the form for different submission targets can only be placed in the onClick event, but this causes the problem that the form will be submitted regardless of whether it passes verification or not. Because this button is of submit type.
My solution to this problem is this:
<script language="JavaScript"><!-- document.returnValue=true;//一个全局变量,给初值。 function validateForm() { var errors;errors='';if (document.PostTopic.title.value=="")errors="标题不能为空";if( document.PostTopic.intro.value.length>10)errors+="\n简介不能多于10个字";if (errors!='') alert(errors);document.returnValue = (errors == '');} file://--></script> <FORM ACTION="" METHOD="post" NAME="PostTopic" onSubmit="return document.returnValue;"><input type=text name=title value=""><input type=text name=intro value=""><INPUT TYPE="submit" NAME=Submit VALUE="新增" class=buttonface onclick="document.PostTopic.action='addone.php';validateForm(); return document.returnValue;"><INPUT TYPE="RESET" NAME=Reset VALUE="重置" class=buttonface ><INPUT TYPE="submit" NAME="Submit" VALUE="修改" class=buttonface onclick="document.PostTopic.action='modify.php';validateForm(); return document.returnValue;"></FORM>