提交联系表格后,如何重定向到页面?

时间:2019-04-01 15:03:16

标签: php

一切正常,除了重定向到“谢谢”页面。我可以向双方发送邮件,但是在提交联系表单后,无法将页面重定向到thankyou.html,我在代码中做错了吗?

session_start();
$serverMail = 'example@gmail.com';

$email = isset($_POST['email']) ? $_POST['email'] : '';
$name = isset($_POST['name']) ? $_POST['name'] : '';
$message = isset($_POST['message']) ? $_POST['message'] : '';
$captcha = isset($_POST['captcha']) ? $_POST['captcha'] : '';
$img_session = isset($_SESSION['img_session']) ? $_SESSION['img_session'] : '';
$website = $_SERVER['mysite'];

$headers = "From: $serverMail\r\n";
$toAdmin = "some example";
$toGuest = "some example";

    if(empty($email) or empty($name) or empty($email) or empty($message)){
        $output = "All fields are required!";
    }
    else{

        //to server side
        if(md5($captcha) == $img_session){
            $mailMessage = "
$name 
some message";

            if (mail($serverMail, $toAdmin, $mailMessage, $headers)) {

            }
        }

        // to guests side
        if(md5($captcha) == $img_session){
            $mailMessage = "
$name 
some message";

            if (mail($email, $toGuest, $mailMessage, $headers)) {
                header('Location:https://stackoverflow.com/');
                exit();
            }
        }
        else{
            $output = "Wrong Captcha Code!";
        }
    }

echo $output;

我必须if(md5($captcha) == $img_session),因为$mailMessage是不同的。

3 个答案:

答案 0 :(得分:2)

无需两次验证验证码。

检查验证码后,生成并发送两封电子邮件,并将状态保留在temp变量中。发送两封电子邮件后,检查两者的状态,并进行相应的编码,以便在发现错误的任何地方进行提取。

session_start();
$serverMail = 'example@gmail.com';

$email = isset($_POST['email']) ? $_POST['email'] : '';
$name = isset($_POST['name']) ? $_POST['name'] : '';
$message = isset($_POST['message']) ? $_POST['message'] : '';
$captcha = isset($_POST['captcha']) ? $_POST['captcha'] : '';
$img_session = isset($_SESSION['img_session']) ? $_SESSION['img_session'] : '';
$website = $_SERVER['mysite'];

$headers = "From: $serverMail\r\n";
$toAdmin = "some example";
$toGuest = "some example";

if(empty($email) or empty($name) or empty($email) or empty($message)){
    $output = "All fields are required!";
} else{

    if(md5($captcha) == $img_session){

        //to server side
        $mailMessage = "\n$name\n\nsome message";

        $m1 = mail($serverMail, $toAdmin, $mailMessage, $headers)) {

        // to guests side
        $mailMessage = "\n$name\n\nsome other message";

        $m2 = mail($email, $toGuest, $mailMessage, $headers)) {

        if ( $m1 && $m2 ) {
            header('Location:https://stackoverflow.com/');
            exit;
        } else {
            // one or both emails failed to send
            if( ! $m1 ) {
                // error message for server mail failure
            }
            if ( ! $m2 ) {
                // error message for guest mail failure
            }
    } else {
        $output = "Wrong Captcha Code!";
    }
}

echo $output;

答案 1 :(得分:1)

尝试使用此代码,如果它不能尝试使用其他浏览器。可能来自

echo '<META HTTP-EQUIV="refresh" CONTENT="0;url=http://helloworld/contact/thankyou.html">';

答案 2 :(得分:0)

因为您在第一封邮件后使用“退出”!