消息发送后重定向到成功页面

时间:2014-09-08 07:28:21

标签: php forms email redirect post

我正在尝试从联系表单发送电子邮件,因此发送电子邮件正在运行但发送后我想重新定向到contact_formsuccess.php,但是它没有重定向到那个页面,它仍然是相同的,可以一个如何使它感谢

<?php
$target_path = "/var/www/xxxxxx/cv/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']); 

//print $target_path; 

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}


$name=$_POST['senderName'];
$email=$_POST['senderEmail'];
$mobile=$_POST['senderMobile'];
$company=$_POST['senderCompany'];
$inquiry=$_POST['inquiry'];
$message=$_POST['message'];

include 'class.phpmailer.php';
include 'class.smtp.php';

$mail = new PHPMailer(); // create a new object
$mail->IsSMTP(); // enable SMTP
$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
$mail->SMTPAuth = true; // authentication enabled
//$mail->SMTPSecure = 'ssl'; // secure transfer enabled REQUIRED for GMail

$mail->Host = "mail.authsmtp.com";
$mail->Port = "25"; // or 587
$mail->IsHTML(true);
$mail->Username = "xxxxx";
$mail->Password = "xxxxx";
$mail->SetFrom("xxx@xxxxxxx.com");

$mail->Subject = $inquiry;
$mail->Body = "Name:$name"."<br/><br />"."E-mail:$email"."<br/><br/>"."Mobile:$mobile"."<br/><br/>"."Company:$company"."<br/><br/>".$message;
$mail->AddAttachment($target_path);
$mail->AddAddress('xxxxxxxxxx@gmail.com');


 if(!$mail->Send())
 {
     echo "Mailer Error: " . $mail->ErrorInfo;
 }
 else
 {
     echo "Message has been sent";

     header('Location: contact_formsuccess.php');
 }  



?>

4 个答案:

答案 0 :(得分:5)

尝试将header('Location: contact_formsuccess.php');放在后面 $mail->AddAddress('xxxxxxxxxx@gmail.com');也许它会起作用

答案 1 :(得分:2)

当您通过header php函数修改HTTP标头时,必须在任何其他输出之前执行此操作。为简化起见,HTTP请求由头部和主体组成。当您致电header时,它将修改HTTP标头部分。当您执行任何输出时(此处为echo "Message has been sent";,它将&#34;关闭&#34;标题部分并填充正文部分。这就是您在输出后无法更改HTTP标头的原因。

您应该尝试删除echo行(以及在调用header之前可以打印的任何输出)并保留header部分。

答案 2 :(得分:0)

如果您要重定向到其他网页而不是移除echo message,因为它会为您创建问题或尝试使用此类代码将用户发送到其他网页。

if (headers_sent()) {
   echo("<script>location.href = 'user.php';</script>");
} else {
   exit(header("Location: user.php"));
}

答案 3 :(得分:0)

你总是可以用JavaScript做到这一点

 if(!$mail->Send())
 {
     echo "Mailer Error: " . $mail->ErrorInfo;
 }
 else
 {
     echo "Sending message(s)...";

    echo "<SCRIPT type='text/javascript'>
            window.location='contact_formsuccess.php?updated=true&updatedmsg=" . urlencode('Message has been sent.') . "';
        </script>";

 }