【发布时间】:2012-03-20 02:58:53
【问题描述】:
在过去的几天里,我一直在尝试修改我在 Internet 上找到的 PHP 表单(我可以看到它非常流行,因为我看到了许多关于该表单的帖子)。 提交表单后,它会向我发送一封电子邮件 - 这太棒了。但是我一直在尝试改变其他一些事情,但根本找不到正确/有效的解决方案。
我想要实现的是:
1) 提交表单时接收电子邮件(我认为这已完成)
2) 验证
3) 在同一页面中显示错误消息
姓名 *
(您的姓名是必填项)
[.......这是文本字段.......]
4) 只允许提交一次表格 - 当所有错误都已更正或所有数据第一次正确输入时。
5) 当验证数据时出现错误消息(之前)时,我的页脚没有显示。这会是什么原因?提交表单时显示正常。
这是我一直在尝试修改的代码(我复制了“原始”代码,因为我的代码可能太乱了,而且在我玩过它之后也发生了太大的变化)
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "you@yourdomain.com";
$email_subject = "Your email subject line";
function died($error) {
// your error code can go here
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 />";
die();
}
// validation expected data exists
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);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
?>
<!-- include your own success html here -->
Thank you for contacting us. We will be in touch with you very soon.
<?php
}
?>
下面是我来自contact.php的部分代码(表单所在的地方)
<form name="contactform" method="post" action="submit_form.php">
<div class="left_form">
<label for="name" class="label">your name</label>
(this is the code that was suppose to display error message within the same page
<?php if($errors['name'] = "") echo $errors['name']; ?>)
<input type="text" class="inputtext" name="name" value="" />
上面的代码对于所有的输入字段几乎都是一样的(唯一不同的是name="")
如果有人可以帮助我,我将非常感激。
谢谢。 =)
【问题讨论】:
标签: php forms validation error-handling