【发布时间】:2015-06-09 13:21:33
【问题描述】:
我有一个表单,for 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._%+-]+@[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