【问题标题】:PHP - Always need to fill in the blank in form validationPHP - 在表单验证中总是需要填写空白
【发布时间】:2014-02-11 10:11:59
【问题描述】:

我正在尝试实现一些 javascript 表单验证。我正在尝试测试表单中的空白字段并使用警报来警告用户,但这对我不起作用当我提交完整填写的表单时,它一直要求我填写空白。

这是我的编码。希望有人可以追踪错误。也许有很多我不明白的错误。非常感谢

<html>
<head>  
</head>

<body>
    <br>
    <h1 align="center">REGISTRATION

    <br>
    <font style="font-size:20px">Vendor's Information</font></h1>
    <div style="padding-left: 225px">
    <br>

    <script type="text/javascript">
    function form_validate()
    {
      with(document.form1)
      {
      if(VENDOR_NAME.value.empty( )) 
      {
        alert("Please fill in the vendor's name");
        VENDOR_NAME.focus();
        VENDOR_NAME.value="";
        return false;
      }
       if(VENDOR_PHONENUM.value.match( )) 
       {
        alert("Please fill in the vendor's contact number");
        VENDOR_PHONENUM.focus();
        VENDOR_PHONENUM.value="";
        return false;
       }
       if(VENDOR_PIC.value.match( )) 
       {
        alert("Please fill in the vendor's person in-charge");
        VENDOR_PIC.focus();
        VENDOR_PIC.value="";
        return false;
       }
       return true;
     }

     }
    </script>

    <form name="form1" method="post" action="vendor_registration.php" onSubmit="return form_validate()">
    <font face="Verdana">

    <table border="0">
    <tr style="font-size:15px">
        <td>Name</td>
        <td>: <input name="VENDOR_NAME" type="text" size="50"><br></td>
    </tr>
    <tr style="font-size:15px">
        <td>Phone No. </td>
        <td>: <input name="VENDOR_PHONENUM" type="text" size="50"><br></td>
    </tr>
    <tr style="font-size:15px">
        <td>Person In-Charge</td>
        <td>: <input name="VENDOR_PIC" type="text" size="50"><br></td>
    </tr>

    </table>
    </font>
    <br/><br/>
    <p align="center">
    <input type="submit" name="submit" value="Submit" style="font-size: 15px;" >
    <input type="reset" name="reset" value="Reset" style="font-size: 15px;">
    </p>
</div>
<br>
<br>
 </body>
</html>

【问题讨论】:

  • 尝试检查浏览器的控制台以获取任何错误详细信息
  • 使用长度而不是空。所以如果length == 0,你可以触发空事件
  • 您的表单可能甚至没有提交...继续进行 JS 验证,或者忽略它,因为这里没有 PHP 代码可以继续。所看到的只是一些(我很抱歉)糟糕的 JS 代码。你期望VENDOR_PIC.value.match( ) 的结果是什么?

标签: javascript php forms validation


【解决方案1】:

尝试对空字段检查进行简单验证,

    <script type="text/javascript">
    function form_validate()
    {
      with(document.form1)
      {
      if(VENDOR_NAME.value == '') 
      {
        alert("Please fill in the vendor's name");
        VENDOR_NAME.focus();
        return false;
      }
       if(VENDOR_PHONENUM.value == '') 
       {
        alert("Please fill in the vendor's contact number");
        VENDOR_PHONENUM.focus();
        return false;
       }
       if(VENDOR_PIC.value == '') 
       {
        alert("Please fill in the vendor's person in-charge");
        VENDOR_PIC.focus();
        return false;
       }
       return true;
     }

     }
    </script>

【讨论】:

  • @Dave 谢谢你,戴夫。我已经运行了您的代码,但问题仍然存在。也许流程代码有问题。呼呼救命~我把过程代码贴出来
【解决方案2】:

`

    include 'db_connect.php';
    $query = "INSERT INTO vendor(VENDOR_NAME, VENDOR_PHONENUM, VENDOR_PIC)
              VALUES ('$VENDOR_NAME', '$VENDOR_PHONENUM', '$VENDOR_PIC')";

    $result = mysql_query($query) or die (mysql_error());

    if($result) 
    {
        echo "<script>alert('Registration Successful!'); </script>";
        $url = 'vendor_view.php';
        echo '<META HTTP-EQUIV=Refresh CONTENT="0; URL='.$url.'">';

        /*echo "<script>alert (\"Registration Successful\")</script>";*/
        /*header('Location: vendor_view.php');*/
    }
    else
        /*echo "<script>alert (\"Registration Failed\")</script>";*/
        echo "<script>alert('Registration Failed!'); </script>";

}

?>`

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多