我有一个表单和m的行为是相同的页面。 我想:为什么此验证代码无法按预期工作?
- 在成功提交表单显示一个感谢的消息旁边,在那里检测到错误
- 上述所有必须在同一页面中显示的字段
- 显示错误消息。
我的代码是:
<?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._%+-][email protected][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
}
?>
我已经限制了验证,并只显示了在这个问题上的第一部分。
当我提交此表,如果没有任何错误,我收到消息谢谢,我们已经收到您的反馈 这是罚款和按预期工作。
当出现错误/字段客人姓名为空我在表单提交过程中收到消息错误! 请检查下面有错误的字段。错误提示以红色显示。 这也很好。
但我的窗体正好消失当我得到上面的消息。为什么? 也想显示请输入您的姓名!旁边的领域。
感谢您的解释.. – 2015-05-12 16:47:22