【问题标题】:Validate the form in php在php中验证表单
【发布时间】:2017-05-26 01:34:44
【问题描述】:

我在 php 中有一个表单,我需要验证该表单。所以我尝试了这些代码,但没有收到任何验证消息。什么都没有显示。我得到的错误是什么

<form class="" method="post" action="">
<div class ="col-md-3" style="clear:both;">
            <label style="font-size:12px;"> Your Name:</label>
            <input type="text" id="AS_Name" name="AS_Name" />
            <span style="font-size:12px; color:red;"> <?php echo $nameError; ?> </span>
            </div>
    <input  id="AstroSend" type="submit" Value="Send" name="submit" />
   </form>

<?php
 $nameError ="";
if(isset($_POST['submit'])) {

if (empty($_POST["AS_Name"]))
    {   

    $nameError = "Name is required";

    } 
else 
    {

    $name = test_input($_POST["AS_Name"]);
    // check name only contains letters and whitespace
    if (!preg_match("/^[a-zA-Z ]*$/",$name)) 
    {
    $nameError = "Only letters and white space allowed";
    }
    } ?>

如果我在if 循环内回显,那么它会显示消息。但我希望 span 本身中的验证消息。

【问题讨论】:

  • 您是否尝试过 $_POST 以查看表单是否已发布
  • 是的,它已发布,我可以进入if 循环,我可以在那里回显消息
  • 将php代码移到顶部
  • 我尝试将 php 移到顶部。但是当我点击提交时,表单将消失
  • 您在if(isset($_POST['submit'])) { 之后打开一个括号,但您从不关闭它。了解如何格式化代码。只需按照您的方式使用您的制表符,您就会在 2 秒后看到此错误。

标签: php validation


【解决方案1】:

您只需要检查 html 上方而不是下方的验证 :)

【讨论】:

    【解决方案2】:

    进行以下更改

    <?php
     $nameError ="";
     if(isset($_POST['submit'])) 
     {
       if (empty($_POST["AS_Name"]))
       {   
        $nameError = "Name is required";
       }
    
      else 
     {
    
      $name = test_input($_POST["AS_Name"]);
    // check name only contains letters and whitespace
         if (!preg_match("/^[a-zA-Z ]*$/",$name)) 
         {
           $nameError = "Only letters and white space allowed";
         }
       } 
      }
    ?>
    
    <form class="" method="post" action="">
    <div class ="col-md-3" style="clear:both;">
            <label style="font-size:12px;"> Your Name:</label>
            <input type="text" id="AS_Name" name="AS_Name" />
            <span style="font-size:12px; color:red;"> <?php echo $nameError;?> </span>
              </div>
           <input  id="AstroSend" type="submit" Value="Send" name="submit" />
           </form>
    

    【讨论】:

      【解决方案3】:

      我不是投反对票的人,但在你的 else 中,@coder 的位置是错误的。

      我工作得很好

      NewBie 可以请您检查一下

      <?php
      $nameError ="";
      if(isset($_POST['submit'])) 
      

      {

      if (empty($_POST["AS_Name"]))
      {   
      $nameError = "Name is required";
      

      } 别的 { $name = $_POST["AS_Name"];

      if (!preg_match("/^[a-zA-Z ]*$/",$name)) 
      

      {

      $nameError = "Only letters and white space allowed";
      

      }

      } 
      

      }

      ?>
      <form class="" method="post" action="">
      
      <div class ="col-md-3" style="clear:both;">
                  <label style="font-size:12px;"> Your Name:</label>
              <input type="text" id="AS_Name" name="AS_Name" />
              <span style="font-size:12px; color:red;"> <?php echo $nameError;?> </span>
      
      </div>
      <input  id="AstroSend" type="submit" Value="Send" name="submit" />
         </form>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-02-13
        • 1970-01-01
        • 2011-07-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多