PHP javascript表单验证未通过而表单依然提交
如题,我的情况是不输入任何数据,点击提交注册,会弹出“请输入用户名”这样的提示,点击确定后 就转到另一个页面,并只显示一句话:Column 'UserName' cannot be null。
下面是代码:
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><?php require_once('Connections/conetion.php'); ?> <?php function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue; switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } $editFormAction = $_SERVER['PHP_SELF']; if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); } if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) { $insertSQL = sprintf("INSERT INTO member (UserName, Password, Email) VALUES (%s, %s, %s)", GetSQLValueString($_POST['UserName'], "text"), GetSQLValueString($_POST['Password'], "text"), GetSQLValueString($_POST['Email'], "text")); mysql_select_db($database_conetion, $conetion); $Result1 = mysql_query($insertSQL, $conetion) or die(mysql_error()); $insertGoTo = "index.php"; if (isset($_SERVER['QUERY_STRING'])) { $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; $insertGoTo .= $_SERVER['QUERY_STRING']; } header(sprintf("Location: %s", $insertGoTo)); } ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>无标题文档</title> <link rel="stylesheet" type="text/css" href="CSS/Web.css"> </head> <body style="padding:0 2.5%;"> <div class="header"></div><!--头部--> <div class="center"><!--导航--> <div class="left"></div> <div style="height:35px; line-height:35px; float:left;"><a href="/">论坛</a></div> <div class="right"></div> </div> <div class="regist"> <h3>注册</h3> </div> <div id="xsnazzy"><!--圆角边框--> <b class="xtop"><b class="xb1"></b><b class="xb2"></b><b class="xb3"></b><b class="xb4"></b></b> <div class="xboxcontent"> <div><h4>请填写以下必填信息完成注册</h4></div> <div style="padding-left:20%; color:#FF0000;">带*的都是必填项目,若填写不全将无法注册</div> <form action="<?php echo $editFormAction; ?>" method="POST" name="form1"> <div style="text-align:center;"> <table class="registTable" width="800"> <tr> <td width="98">用户名*</td> <td width="182"><input type="text" name="UserName" style="height:15px; width:110px;"/></td> <td width="504">不能有空格,可以是中文,长度控制在 3-15 字节以内 </td> </tr> <tr> <td>密 码*</td> <td><input type="text" name="Password" id="Password" style="height:15px; width:110px;"/></td> <td>最小长度:3 最大长度:16 </td> </tr> <tr> <td></td> <td><hr style="height:5px; background-color:#E5E9EB; width:115px;" /></td> <td></td> </tr> <tr> <td>确认密码*</td> <td><input type="text" name="checkPassword" id="checkPassword" style="height:15px; width:110px;"/></td> <td>请再输入一遍您上面填写的密码 </td> </tr> <tr> <td>Email*</td> <td><input type="text" name="Email" style="height:15px; width:110px;"/></td> <td>请填写真实并且最常用的邮箱</td> </tr> <tr> <td></td> <td> </td> <td></td> </tr> <tr> <td></td> <td> <input type="submit" value="提交注册" class="btn" onclick="checkForm()" /> <script type="text/javascript"> function checkForm(){ var p1=document.form1.Password.value; var p2=document.form1.checkPassword.value; if(document.form1.UserName.value==''){ alert('请输入用户名'); document.form1.UserName.focus(); return false; } else if(p1==''){ alert('请输入密码!'); document.form1.Password.focus(); return false; } else if(p1!=p2){ alert('确认密码不正确'); document.form1.checkPassword.focus(); return false; } return true; } </script> <input type="reset" value="重置" class="bt" /> </td> <td></td> </tr> </table> </div> <input type="hidden" name="MM_insert" value="form1"> </form> </div> <b class="xbottom"><b class="xb4"></b><b class="xb3"></b><b class="xb2"></b><b class="xb1"></b></b> </div> <div style="height: 4px; font: 0; line-height: 4px;"></div> <div class="foot">Cookie をクリアする お問い合わせ 画像なしバージョン モバイルで参照 トップに戻る </div> <div style=" text-align:center;"> <p>現在の時刻:06-21 21:28、Gzip が有効です Zhejiang B2-20070029<br /> phpwind v7.5 SP3 証明書コード @2003-2010 phpwind.com Corporation </p> を利用しています。 <p></p> </div> </ボディ> </html> <div class="clear"></div>