2

I have written this form, and it works perfectly. It sends the email fine. But after it sends, it errors. So people submitting the form think its broken, but it actually sends the form. All I am trying to do is redirect after the form submits to thanks.asp after the mail sends.

Any help is appreciated.

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

2 回答 2

0

我相信问题是你使用header();不正确。

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

PHP 文档 - 标头

于 2013-08-16T19:11:15.300 回答
-2

我建议您使用 javascript 进行重定向

只是改变这个:

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

对此:

echo '<script>
window.location.href = "http://www.macnx.com/noflash/thanks.asp";
</script>';
于 2013-08-16T18:55:29.630 回答