简体   繁体   中英

Redirecting to success page after message send

I am trying to send email from the contact form so sending email is working but after sending I want to re-direct to the contact_formsuccess.php ,but it's not re-directing to that page its remains on the same, can any one how to make it thanks

<?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');
 }  



?>

try to put header('Location: contact_formsuccess.php'); after the $mail->AddAddress('xxxxxxxxxx@gmail.com'); maybe it will works

When you modify the HTTP headers via the header php function, you must do it before any other output. To simplify, a HTTP request is composed of an header and then a body. When you call header , it will modify the HTTP header part. When you do any output (here your echo "Message has been sent"; , it will "close" the header section and fill the body section. This is why you can not change HTTP headers after any output.

You should try to delete your echo line (as well as any output that can be printed before the call to header ) and just keep the header part.

If you are redirecting to other page than remove the echo message because it's createing issue for you or try code something like this to send user to other page.

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

You could always do it in 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>";

 }  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM