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

时间:2013-08-16 18:51:30

标签: php

我写过这个表格,它完美无缺。它发送电子邮件很好。但是在发送之后,它就出错了。因此,提交表单的人认为它已损坏,但它实际上发送了表单。我想要做的就是在邮件发送后表单提交到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");
    }
?>

2 个答案:

答案 0 :(得分:0)

我认为问题在于您错误地使用了header();

  

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

PHP Documentation - header

答案 1 :(得分:-2)

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

只需改变一下:

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

到此:

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