【发布时间】:2012-06-24 17:08:58
【问题描述】:
我有一个联系表格,它应该会转到一个谢谢你的 html 页面。电子邮件正在发送到我的帐户,但是在提交页面时它会转到一个空白的 php 页面,而不是转到我的谢谢 html 页面。有什么建议么?代码很低。
<?php
$errors = '';
$myemail = 'yourname@website.com';//<-----Put Your email address here.
if(empty($_POST['name']) ||
empty($_POST['email']) ||
empty($_POST['message']))
{
$errors .= "\n Error: all fields are required";
}
$name = $_POST['name'];
$email_address = $_POST['email'];
$message = $_POST['message'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= "\n Error: Invalid email address";
}
if( empty($errors))
{
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Name: $name \n Email: $email_address \n Message \n $message";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Contact form handler</title>
</head>
<body>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
?>
</body>
【问题讨论】:
-
您不能同时拥有
echo和Header。正如亚历克斯在下面所说,使用他的建议并摆脱回声。您可能会在if语句中使用您的错误消息。 -
您还应该发布自己的
form而不仅仅是echo nl2br($errors);。您可能不需要它,因为它已经在您的处理程序中。 -
感谢您的回复。我想知道是否可以在 php 中包含 html 感谢页面的代码?我没有构建此代码,所以我仍在尝试解决问题。我不确定这样做是否会干扰发送到我帐户的邮件。