【问题标题】:Redirecting to success page after message send消息发送后重定向到成功页面
【发布时间】:2014-09-08 07:28:20
【问题描述】:

我正在尝试从联系表单发送电子邮件,因此发送电子邮件正常,但发送后我想重定向到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');
 }  



?>

【问题讨论】:

  • 打开错误报告,您应该会收到警告:“标头已发送”
  • 为什么它还在同一个页面中

标签: php forms email redirect post


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    当您通过header php 函数修改HTTP 标头时,您必须在任何其他输出之前执行此操作。为简化起见,HTTP 请求由标头和正文组成。当您调用header 时,它会修改HTTP 标头部分。当您进行任何输出时(这里是您的echo "Message has been sent";,它将“关闭”标题部分并填充正文部分。这就是为什么您不能在任何输出后更改 HTTP 标题。

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

    【讨论】:

    • 感谢您的回答,您能修改我的代码吗?我尝试删除 echo 但没有重定向
    • move_uploaded_file 之后有一个潜在回声,header 函数之前有一个。
    • 我试过这样我删除了所有的回声,只是放了 header("Location: contact_formsuccess.php");它重新编辑但邮件不发送
    • 那么这不是重定向的问题,而是邮件发送。请使用您的新问题描述打开另一个问题。
    【解决方案3】:

    如果您要重定向到其他页面,而不是删除 echo message,因为它会给您带来问题,或者尝试编写类似这样的代码以将用户发送到其他页面。

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

    【讨论】:

    • 这假设javascript被激活。并非总是如此(例如,脚本不是从浏览器调用的)。
    • 是的,我知道,但根据@xavi 的当前问题/脚本,它会起作用。
    【解决方案4】:

    你总是可以在 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>";
    
     }  
    

    【讨论】:

      猜你喜欢
      • 2012-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-08
      • 1970-01-01
      相关资源
      最近更新 更多