表单提交后标题位置不重定向

时间:2014-11-06 07:28:30

标签: php forms redirect header

您好我的表单在提交后没有重定向到我的成功页面。消息确实通过,我收到测试电子邮件。但是,在发送电子邮件后,我的表单不会重定向。我正在使用标头位置来重定向。任何人都可以帮我这个吗?

require_once('class.phpmailer.php');
session_start();

function ValidateEmail($email) {
$pattern = '/^([0-9a-z]([-.\w]*[0-9a-z])*@(([0-9a-z])+([-\w]*[0-9a-z])*\.)+[a-z]{2,6})$/i';
  return preg_match($pattern, $email);
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$nameError = '';
if (empty($_POST["name"])) {
$nameError = 'You must enter a name.';
} 
if (isset($_POST['captchaCode'],$_SESSION['random_txt']) && md5($_POST['captchaCode']) == $_SESSION['random_txt'])
{
  unset($_POST['captchaCode'],$_SESSION['random_txt']);
} else {
  echo 'captcha code was wrong';
  exit;
}
}

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

$subject = $_POST['subject'];
$body = <<< _END_
<html>
<body>
<div id="container">
<div id="logo">
<a href="http://www.website.com"><img src="images/website.png" alt="website"></a>
</div>
Name: {$_POST['name']} <br>
Phone: {$_POST['phone']} <br>
Email: {$_POST['email']} <br>
Message: {$_POST['message']} <br>
</div>
</html>
_END_;

// if(isset($_POST['submit'])){
$name = $_POST['name'];
$email = $_POST['email'];
$mail = new PHPMailer;
// $mail->SMTPDebug = 2;
// print_r($_POST);
$mail->IsSMTP();
$mail->SMTPAuth = true;
$mail->SMTPSecure = "tls";
$mail->Host = "smtp.office365.com";
$mail->Port = 587;
$mail->Username = "email@website.com";
$mail->Password = "password";

$mailto = "email@website.com";
$mailfrom = "email@website.com";
$mail->SetFrom($mailto, '');
$address = 'email@website.com';
$mail->AddAddress($address, "The Site Name");

$mail->Subject  = $subject;
$mail->AltBody  = $body;
$mail->MsgHTML($body);

if(!$mail->Send()) {
    Header("Location: success.php");
}
}

1 个答案:

答案 0 :(得分:0)

您正在检查错误的情况。如果发送邮件,则重定向。所以它应该是 -

if($mail->Send()) {
    Header("Location: success.php");
    exit;
}