0

我正在制作一个表单来提交数据,但问题是错误没有回显。如果所有字段都输入正确,则表单成功提交数据,但当没有数据时,不会返回错误输出。我把这个回声语句放在标题下面

这是表格...

<label for="Name"> Names:</label>
                  <input type="text" name="Name" maxlength="25" size="24"  class="input"/>

<label for="Email">Email Address:</label>

                              <input type="text" name="Email" maxlength="30" size="24"  class="input"/>

<label for="Subject">Subject:</label><br/>
                              <input type="text" name="Subject" maxlength="25" size="25"  class="input"/>

<label for="Comments">Comments</label><br/>
                              <textarea rows="6"   cols="50" name="Ujumbe"size="140" class="textarea"></textarea>
  <input type="submit" value="Send Mail" name="submit" />

这是验证码...

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

                   if(empty($_POST['Name'])) {  $error[]='Your  name ';  }
                     else if (!empty ($_POST['Name'])){ $Name=mysql_real_escape_string(htmlentities($_POST['Name']));  }

                   if(empty($_POST['Email']))    {$error[]='Your  email address. ';  }

                      else if(preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/",$_POST['Email'])){
                           $Email = mysql_real_escape_string($_POST['Email']);
                          }
                           else {
                                 $error[]='That you entered e-mail address is invalid.';
                                }

                  if(empty($_POST['Comments'])){   $error[]='Enter a comment ';    }
                                         else if (!empty ($_POST['Comments']))
                                        {
                                         $Comments=mysql_real_escape_string(htmlentities($_POST['Comments']));  
                                        }

                  if(empty($_POST['Subject'])){   $error[]='Enter the subject ';    }
                                                   else if (!empty ($_POST['Comments'])){ $Subject=mysql_real_escape_string(htmlentities($_POST['Subject']));  }


      if (empty ($error)){

                header("messages.php?x=3");


                         }
       else{
             $error_message ='<br/><br/><span class="error"><br />You did not submit the following required information!<br/><br/>' ;
             foreach($error as $key => $values) {
                       $error_message.= "<br/>$values<br/>";
                }
                    $error_message.="</span> <br/><br/><br/>";
                            }}

?>
4

1 回答 1

0

将代码的最后一部分更改为如下所示:

if (empty ($error)){
        header("messages.php?x=3");
    } else {
        $error_message ='<br/><br/><span class="error"><br />You did not submit the following required information!<br/><br/>' ;
        foreach($error as $key => $values) {
            $error_message.= "<br/>$values<br/>";
        }

        $error_message.="</span> <br/><br/><br/>";

        echo 'Please see the below error Messages: <br>' . $error_message;
    }

此外,您可以简单地创建一个无序列表,见下​​文,并使用它来分隔列表项,而不是在您的代码中添加无数<br>丑陋不必要的代码。margins

            if (empty ($error)){
            header("messages.php?x=3");
        } else {
            $error_message ='<span class="error">You did not submit the following required information!</span>
<ul id="error-list">
' ;
            foreach($error as $key => $values) {
                $error_message.= "<li>$values</li>";
            }

            $error_message.="</ul>";

            echo 'Please see the below error Messages: <br>' . $error_message;
        }

CSS:

.error{
    margin: 15px 0 10px 0;
}

#error-list li{
    margin-top: 10px;
}
于 2013-04-24T19:11:01.063 回答