Php 表单成功后重定向到其他URL

Php 表单成功后重定向到其他URL,php,Php,我有一个使用PHPMailer的基本表单页面: 一切正常,并发送到相关的电子邮件,但我想把用户推到另一个页面后,成功的消息已经出现,以便我可以在这个新的页面上添加一些跟踪代码 任何帮助都将不胜感激 代码如下: <?php require_once('phpmailer/PHPMailerAutoload.php'); $toemails = array(); $toemails[] = array( 'email' => 'test@test.com', // You

我有一个使用PHPMailer的基本表单页面:

一切正常,并发送到相关的电子邮件,但我想把用户推到另一个页面后,成功的消息已经出现,以便我可以在这个新的页面上添加一些跟踪代码

任何帮助都将不胜感激

代码如下:

 <?php

require_once('phpmailer/PHPMailerAutoload.php');

$toemails = array();

$toemails[] = array(
    'email' => 'test@test.com', // Your Email Address
    'name' => 'Some type of name' // Your Name
);

// Form Processing Messages
$message_success = 'We have <strong>successfully</strong> received your Message and will get Back to you as soon as possible.';

// Add this only if you use reCaptcha with your Contact Forms
$recaptcha_secret = 'your-recaptcha-secret-key'; // Your reCaptcha Secret

$mail = new PHPMailer();

// If you intend you use SMTP, add your SMTP Code after this Line


if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if ($_POST['template-contactform-email'] != '') {

        $name  = isset($_POST['template-contactform-name']) ? $_POST['template-contactform-name'] : '';
        $email = isset($_POST['template-contactform-email']) ? $_POST['template-contactform-email'] : '';
        $phone = isset($_POST['template-contactform-phone']) ? $_POST['template-contactform-phone'] : '';

        $subject = isset($_POST['template-contactform-subject']) ? $_POST['template-contactform-subject'] : '';


        $subject = isset($subject) ? $subject : 'Ladies Boxing Offer';

        $botcheck = $_POST['template-contactform-botcheck'];

        if ($botcheck == '') {

            $mail->SetFrom($email, $name);
            $mail->AddReplyTo($email, $name);
            foreach ($toemails as $toemail) {
                $mail->AddAddress($toemail['email'], $toemail['name']);
            }
            $mail->Subject = $subject;

            $name  = isset($name) ? "Name: $name<br><br>" : '';
            $email = isset($email) ? "Email: $email<br><br>" : '';
            $phone = isset($phone) ? "Phone: $phone<br><br>" : '';


            $referrer = $_SERVER['HTTP_REFERER'] ? '<br><br><br>This Enquiry was submitted from: ' . $_SERVER['HTTP_REFERER'] : '';

            $body = "$name $email $phone $service $message $referrer";

            // Runs only when File Field is present in the Contact Form
            if (isset($_FILES['template-contactform-file']) && $_FILES['template-contactform-file']['error'] == UPLOAD_ERR_OK) {
                $mail->IsHTML(true);
                $mail->AddAttachment($_FILES['template-contactform-file']['tmp_name'], $_FILES['template-contactform-file']['name']);
            }

            // Runs only when reCaptcha is present in the Contact Form
            if (isset($_POST['g-recaptcha-response'])) {
                $recaptcha_response = $_POST['g-recaptcha-response'];
                $response           = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=" . $recaptcha_secret . "&response=" . $recaptcha_response);

                $g_response = json_decode($response);

                if ($g_response->success !== true) {
                    echo '{ "alert": "error", "message": "Captcha not Validated! Please Try Again." }';
                    die;
                }
            }

            $mail->MsgHTML($body);
            $sendEmail = $mail->Send();

            if ($sendEmail == true):
                echo '{ "alert": "success", "message": "' . $message_success . '" }';
            else:
                echo '{ "alert": "error", "message": "Email <strong>could not</strong> be sent due to some Unexpected Error. Please Try Again later.<br /><br /><strong>Reason:</strong><br />' . $mail->ErrorInfo . '" }';
            endif;
        } else {
            echo '{ "alert": "error", "message": "Bot <strong>Detected</strong>.! Clean yourself Botster.!" }';
        }
    } else {
        echo '{ "alert": "error", "message": "Please <strong>Fill up</strong> all the Fields and Try Again." }';
    }
} else {
    echo '{ "alert": "error", "message": "An <strong>unexpected error</strong> occured. Please Try Again later." }';
}

?>

您可以将这一行放在代码中:

echo "<script>window.location='successPage.php'</script>";
echo“window.location='successPage.php';
从而成为成功的一部分:

if ($sendEmail == true):
  echo '{ "alert": "success", "message": "'.$message_success.'" }';
  echo "<script>window.location='successPage.php'</script>";

else :
  echo '{ "alert": "error", "message": "Email <strong>could not</strong> be sent due to some Unexpected Error. Please Try Again later.<br /><br /><strong>Reason:</strong><br />'.$mail - > ErrorInfo.'" }';

endif;
if($sendmail==true):
回显“{”警报“:”成功“,”消息“:”.$message_success.”“}”;
echo“window.location='successPage.php';
其他:
回显“{”警报“:”错误“,”消息“:”由于某些意外错误,电子邮件无法发送。请稍后重试。

原因:
.$mail->ErrorInfo.'''}”; endif;
您可以尝试:

if ($sendEmail == true):
                echo '{ "alert": "success", "message": "' . $message_success . '" }';
                die('<script type="text/javascript">window.location.href=somepage.html;</script>');

我试过你的建议,但似乎不起作用=(不,不幸的是表单就在那里:我已经删除了额外的重定向代码,以便您可以查看它当前的工作方式。我看到第一个代码不工作,请将第二个与标题一起使用。好的,我收到以下错误:Ladys boxing.html:1访问位于“”的XMLHttpRequest(从“”重定向)来自原点“”已被CORS策略阻止:对飞行前请求的响应未通过访问控制检查:请求的资源上不存在“访问控制允许原点”标头。
if ($sendEmail == true):
                echo '{ "alert": "success", "message": "' . $message_success . '" }';
                header('Location: somepage.html');