【问题标题】:Why this validation code does not work as expected?为什么这个验证码不能按预期工作?
【发布时间】:2015-06-09 13:21:33
【问题描述】:

我有一个表单,for m 的操作是同一页。 我正在尝试:

  1. 表单提交成功后显示感谢信息
  2. 在检测到错误的字段旁边显示错误消息
  3. 以上所有内容必须显示在同一页面中。

我的代码是:

<?php
    $errors = array();
    if (isset($_POST["Ask_the_Question"])) {
        $guest_name = $_POST["guest_name"];
        $title = $_POST["faq_title"];
        $description = $_POST["faq_desc"];
        $title = $_POST["faq_title"];

        /* validation */
        if (empty($guest_name)) {
            $errors['guest_name'] = "Please type your name!";
        }   

        if(!empty($errors)){ echo '<h1 style="color: #ff0000;">Errors!</h1><h6 style="color: #ff0000;">Please check the fields which have errors below. Error hints are in Red.</h6>';}

        if(empty($errors)){     
            echo 'Thanks, We have received your feed back';
        }
    }

    else {
?>
            <form action="index.php" method="post" class="booking_reference">
                <input type="text" name="guest_name" placeholder="Your Name" value="<?PHP if(!empty($errors)) { echo $guest_name;} ?>" />
                <?php if(isset($errors['guest_name'])) { echo '<span style="color: red">'.$errors['guest_name'].'</span>'; } ?>
                <input type="email" name="guest_email" placeholder="Your email" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$" required />
                <input type="text" name="faq_title" placeholder="FAQ Title"/>
                <input type="text" name="faq_desc" placeholder="FAQ Description"/>
                <input type="submit" name="Ask_the_Question" value="Ask the Question" />
            </form>
<?php
        }
?>

我已限制验证并仅显示此问题的第一部分。

当我提交此表单时,如果没有任何错误,我会收到消息谢谢,我们已收到您的反馈 这很好,可以按预期工作。

当存在错误/Guest name 字段为空时,我在表单提交期间收到消息错误! 请检查以下有错误的字段。错误提示为红色。 也不错。

但是当我收到上述消息时,我的表单就消失了。为什么? 我还想在字段旁边显示请输入您的姓名!

【问题讨论】:

    标签: php arrays forms validation


    【解决方案1】:

    只需删除else 条件,因为实际上如果设置了$_POST["Ask_the_Question"],您的表单将不会显示

    <?php
        $errors = array();
        if (isset($_POST["Ask_the_Question"])) {
            $guest_name = $_POST["guest_name"];
            $title = $_POST["faq_title"];
            $description = $_POST["faq_desc"];
            $title = $_POST["faq_title"];
    
            /* validation */
            if (empty($guest_name)) {
                $errors['guest_name'] = "Please type your name!";
            }   
    
            if(!empty($errors)){ echo '<h1 style="color: #ff0000;">Errors!</h1><h6 style="color: #ff0000;">Please check the fields which have errors below. Error hints are in Red.</h6>';}
    
            if(empty($errors)){     
                echo 'Thanks, We have received your feed back';
            }
        }
                <form action="index.php" method="post" class="booking_reference">
                    <input type="text" name="guest_name" placeholder="Your Name" value="<?PHP if(!empty($errors)) { echo $guest_name;} ?>" />
                    <?php if(isset($errors['guest_name'])) { echo '<span style="color: red">'.$errors['guest_name'].'</span>'; } ?>
                    <input type="email" name="guest_email" placeholder="Your email" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$" required />
                    <input type="text" name="faq_title" placeholder="FAQ Title"/>
                    <input type="text" name="faq_desc" placeholder="FAQ Description"/>
                    <input type="submit" name="Ask_the_Question" value="Ask the Question" />
                </form>
    

    【讨论】:

      【解决方案2】:

      原因在这里:

      <?php
      if (isset($_POST["Ask_the_Question"])) {
              $guest_name = $_POST["guest_name"];
              $title = $_POST["faq_title"];
              $description = $_POST["faq_desc"];
              $title = $_POST["faq_title"];
      
              /* validation */
              if (empty($guest_name)) {
                  $errors['guest_name'] = "Please type your name!";
              }   
      
              if(!empty($errors)){ echo '<h1 style="color: #ff0000;">Errors!</h1><h6 style="color: #ff0000;">Please check the fields which have errors below. Error hints are in Red.</h6>';}
      
              if(empty($errors)){     
                  echo 'Thanks, We have received your feed back';
              }
          } else {
            // your form code will never be called if $_POST['Ask_the_Question'] is set
      

      要做你想做的事,你可能想做这样的事情:

      <?php
          $errors = array();
          if (isset($_POST["Ask_the_Question"])) {
              $guest_name = $_POST["guest_name"];
              $title = $_POST["faq_title"];
              $description = $_POST["faq_desc"];
              $title = $_POST["faq_title"];
      
              /* validation */
              if (empty($guest_name)) {
                  $errors['guest_name'] = "Please type your name!";
              }   
      
              if(!empty($errors)){ echo '<h1 style="color: #ff0000;">Errors!</h1><h6 style="color: #ff0000;">Please check the fields which have errors below. Error hints are in Red.</h6>';}
           }
      
           if(empty($errors)){     
                echo 'Thanks, We have received your feed back';
            } else { ?>
                  <form action="index.php" method="post" class="booking_reference">
                      <input type="text" name="guest_name" placeholder="Your Name" value="<?PHP if(!empty($errors)) { echo $guest_name;} ?>" />
                      <?php if(isset($errors['guest_name'])) { echo '<span style="color: red">'.$errors['guest_name'].'</span>'; } ?>
                      <input type="email" name="guest_email" placeholder="Your email" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$" required />
                      <input type="text" name="faq_title" placeholder="FAQ Title"/>
                      <input type="text" name="faq_desc" placeholder="FAQ Description"/>
                      <input type="submit" name="Ask_the_Question" value="Ask the Question" />
                  </form>
                 <?php
      
              }
          }
      
      ?>
      

      【讨论】:

        【解决方案3】:

        试试下面的代码。我已经删除了 else 部分并使用 true/false 设置标志来检查是否有效。

        <?php
            $errors = array();
            if (isset($_POST["Ask_the_Question"])) {
                $guest_name = $_POST["guest_name"];
                $title = $_POST["faq_title"];
                $description = $_POST["faq_desc"];
                $title = $_POST["faq_title"];
        
                /* validation */
                $chkValidate = "true";
                if (empty($guest_name)) {
                    $errors['guest_name'] = "Please type your name!";
                    $chkValidate = "false";
                }   
        
                if(!empty($errors)){ echo '<h1 style="color: #ff0000;">Errors!</h1><h6 style="color: #ff0000;">Please check the fields which have errors below. Error hints are in Red.</h6>';
                    $chkValidate = "false";
                }
        
                if($chkValidate == "true"){     
                    echo 'Thanks, We have received your feed back';
                }
            }
            ?>
                    <form action="index.php" method="post" class="booking_reference">
                        <input type="text" name="guest_name" placeholder="Your Name" value="<?php if(!empty($errors) && $chkValidate != "false") { echo $guest_name;} ?>" />
                        <?php if(isset($errors['guest_name'])) { echo '<span style="color: red">'.$errors['guest_name'].'</span>'; } ?>
                        <input type="email" name="guest_email" placeholder="Your email" pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$" required  />
                        <input type="text" name="faq_title" placeholder="FAQ Title"/>
                        <input type="text" name="faq_desc" placeholder="FAQ Description"/>
                        <input type="submit" name="Ask_the_Question" value="Ask the Question" />
                    </form>
        <?php
        
        ?>
        

        【讨论】:

          【解决方案4】:

          其他答案很好,但只是为了澄清会发生什么。

          但是当我收到上述消息时,我的表单就消失了。为什么?

          您的表单消失了,因为如果您通过第一个 if,您将无法访问您的 else

          if (isset($_POST["Ask_the_Question"])) {
              ...
          } else {
              xxx;
          }
          

          这意味着如果你想看到你的表单,你必须把它放在某个地方,它可以显示为elseif(有更多限制),或者是内部或外部。

          if (isset($_POST["Ask_the_Question"]) && empty($errors)) {
              ...
          } elseif (isset($_POST["Ask_the_Question"]) && !empty($errors)) {
              ...
          } else {
              ...
          }
          

          我还想显示请输入您的姓名!在田野旁边。

          要显示您可以使用的所有错误,例如。 foreach 任何你想展示的地方。

          foreach ($errors as &$error) {
              echo "Error: $error<br />\n";
          }
          

          顺便提一下empty(); function

          【讨论】:

            最近更新 更多