我正在尝试在同一页面上使用 php 错误检查来构建表单。
除了正确填写表单后将访问者重定向到我网站上的另一个页面(thanks.html)外,一切似乎都运行良好。首先,一封电子邮件被发送到我的代码顶部提供的电子邮件地址,之后访问者应该被重定向到感谢页面,我无法开始工作。
重定向是我代码中的这一部分:
/* Redirect visitor to the thank you page */
header('Location: thanks.html');
这是我总共拥有的:
<?php
/* Set e-mail recipient */
$myemail = "myemail@gmail.com";
if (!$_POST['submit'])
{
form();
} else {
if (empty($_POST['name'])) { $error0='<br>name'; }
if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['email_address'])) { $error2='<br>Valid email address'; }
if (empty($_POST['phonenumber'])) { $error1='<br>phonenumber'; }
if (empty($_POST['comment'])) { $error3='<br>comment'; }
$error_messages = $error0.$error1.$error2.$error3;
if ($error_messages)
{
echo "Please ensure the following fields are completed before submitting your form:<strong>". $error_messages ."</strong><br><br>";
form();
} else {
$name = $_POST['name'];
$email_address = $_POST['email_address'];
$phonenumber = $_POST['phonenumber'];
$comment = $_POST['comment'];
/* Message for the e-mail */
$message = "
New contact request:
Name: $name
Email address: $email_address
Phonenumber: $phonenumber
comment: $comment
";
$subject = "Contact";
$headers = "From: $email_address";
/* Send the message using mail() function */
mail($myemail, $subject, $message, $headers);
/* Redirect visitor to the thank you page */
header('Location: thanks.html');
}}
?>
检查错误时,我得到以下结果:
Warning: Cannot modify header information - headers already sent by (output started at form.php:11).
Form.php:11 就是这行代码:
if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['email_address'])) { $error2='<br>Valid email address'; }
因此,在查看 stackoverflow 上的其他帖子后,我猜问题可能是空格或已发送的标头。但我仍然无法正常工作。
有人可以帮忙并提出解决此问题的建议吗?