我是 PHP 新手。在将联系表发送到公司电子邮件后,我正在尝试重定向到感谢页面。但是,我收到一条错误消息:
Warning: Cannot modify header information - headers already sent by (output started at /home/content/86/11388686/html/contact-form-handler.php:6) in /home/content/86/11388686/html/contact-form-handler.php on line 37.
表单确实发送到电子邮件并且代码确实验证了,但重定向不起作用。就像我说的那样,这对我来说是新的,所以请非常具体地回答。谢谢
<?php
/* Set e-mail recipient */
$myemail = "tsunami.transport@aol.com";
/* Check all form inputs using check_input function */
$name = check_input($_POST['name'], "Enter your name");
$subject = check_input($_POST['subject'], "Enter a subject");
$email = check_input($_POST['email']);
$message = check_input($_POST['message'], "Write your message");
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}
/* Let's prepare the message for the e-mail */
$message = "
Name: $name
E-mail: $email
Subject: $subject
Message:
$message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: http://www.tsunamitransport.com/contact-form-thank-you.html');
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>
</body>
</html>
<?php
exit();
}
?>