【发布时间】:2014-03-05 15:14:48
【问题描述】:
我正在创建一个提交联系表单。但是,当使用不正确的信息按下提交按钮时,我希望它返回到我的contact.php 页面,并在正文页面顶部显示附加错误消息,例如。 “我们很抱歉,您提交的表格有错误,这些错误出现在下面: 不正确的电子邮件 不完整的姓氏”等
我有两个文件,contact.php 用于表单,send_form_email.php 用于电子邮件处理
在我的联系人文件中,我有以下代码;
$inc = $_GET['inc'];
if($inc ==1) {
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";}
?>
在我的 send_form_email.php 中,我有以下代码
function died($error) {
header("location: contact.php?inc=1");
die();
}
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$comments = $_POST['comments']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';}
if(strlen($error_message) > 0) {
died($error_message);}
但是,当我输入错误的名字时,错误信息不会出现。
你能帮帮我吗?
问候,莱克斯
【问题讨论】:
-
什么是
died?可能是出了什么问题。在此处查看示例代码:ideone.com/CZp0pz -
echo $error."
"; >> 此代码在contact.php 中不起作用。其余的都在起作用。 -
你在测试什么名字?