提交表单后不会重定向-表单有效,仍然会出错


Will not redirect after submitting form - Form works, still get error

我已经写了这个表单,它运行得很好。它发送电子邮件很好。但在发送之后,它会出错。所以提交表单的人认为它坏了,但它实际上发送了表单。我所要做的就是在表单提交后重定向到邮件发送后的thanks.asp。

感谢您的帮助。

<?php
    $name = trim($_POST['name']);
    $company = $_POST['company'];
    $email = $_POST['email'];
    $comments = $_POST['comments'];
    $site_owners_email = 'support@macnx.com'; // Replace this with your own email address
    $site_owners_name = 'Landin'; // replace with your name
    if (strlen($name) < 2) {
        $error['name'] = "Please enter your name";  
    }
    if (!preg_match('/^[a-z0-9&'''.'-_'+]+@[a-z0-9'-]+'.([a-z0-9'-]+'.)*+[a-z]{2}/is', $email)) {
        $error['email'] = "Please enter a valid email address"; 
    }
    if (strlen($comments) < 3) {
        $error['comments'] = "Please leave a comment.";
    }
    if (!$error) {
        require_once('phpMailer/class.phpmailer.php');
        $mail = new PHPMailer();
        $mail->From = $email;
        $mail->FromName = $name;
        $mail->Subject = "Website Contact Form";
        $mail->AddAddress($site_owners_email, $site_owners_name);
        $mail->Body = $email . $company . $comments;
        // GMAIL STUFF
        $mail->Mailer = "smtp";
        $mail->Host = "smtp.gmail.com";
        $mail->Port = 587;
        $mail->SMTPSecure = "tls"; 
        $mail->SMTPAuth = true; // turn on SMTP authentication
        $mail->Username = "inquiry@getgearhead.com"; // SMTP username
        $mail->Password = "..."; // SMTP password
        $mail->Send();
        header("Location: http://www.macnx.com/noflash/thanks.asp");
    }
?>

我认为问题在于您使用header();不正确。

请记住,在发送任何实际输出之前,必须调用header(),无论是通过普通的HTML标记、文件中的空行还是从PHP发送。

PHP文档-标题

i建议您使用javascript重定向

只需更改一下:

header("Location: http://www.macnx.com/noflash/thanks.asp");

到此:

echo '<script>
window.location.href = "http://www.macnx.com/noflash/thanks.asp";
</script>';