我正在制作一个提交数据的表单,但问题是错误没有得到回应。如果正确输入了所有字段,则表单成功提交数据,但是当没有数据时,不返回错误输出。我把这个echo语句放在标题
下面这是表格......
<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/>";
}}
?>
答案 0 :(得分: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;
}