PHP帮助将联系表单重定向到感谢页面


php help redirect contact form to thank you page

目前我的联系表单回复了一条消息。我想更改它,以便它重定向到感谢页面。有人可以帮我相应地调整我的代码吗?

我知道我需要做 window.location 或 headerLocation,但我不确定在这个特定模板中的哪个位置插入它以使其工作。

<?php
include('SMTPClass.php');
$use_smtp = '0';
$emailto = 'email@email.com';
    // retrieve from parameters
    $emailfrom = isset($_POST["email"]) ? $_POST["email"] : "";
    $nocomment = isset($_POST["nocomment"]) ? $_POST["nocomment"] : "";
    $subject = 'Inquiry';
    $message = '';
    $response = '';
    $response_fail = 'There was an error verifying your details.';
        // Honeypot captcha
        if($nocomment == '') {
            $params = $_POST;
            foreach ( $params as $key=>$value ){
                if(!($key == 'ip' || $key == 'emailsubject' || $key == 'url' || $key == 'emailto' || $key == 'nocomment' || $key == 'v_error' || $key == 'v_email')){
                    $key = ucwords(str_replace("-", " ", $key));
                    if ( gettype( $value ) == "array" ){
                        $message .= "$key: 'n";
                        foreach ( $value as $two_dim_value )
                        $message .= "...$two_dim_value<br>";
                    }else {
                        $message .= $value != '' ? "$key: $value'n" : '';
                    }
                }
            }
        $response = sendEmail($subject, $message, $emailto, $emailfrom);
        } else {
            $response = $response_fail;
        }
    echo $response;
// Run server-side validation
function sendEmail($subject, $content, $emailto, $emailfrom) {
    $from = $emailfrom;
    $response_sent = 'Thank you. Your messsage has been received.';
    $response_error = 'Error. Please try again.';
    $subject =  filter($subject);
    $url = "Origin Page: ".$_SERVER['HTTP_REFERER'];
    $ip = "IP Address: ".$_SERVER["REMOTE_ADDR"];
    $message = $content."'n$ip'r'n$url";
    // Validate return email & inform admin
    $emailto = filter($emailto);
    // Setup final message
    $body = wordwrap($message);
    if($use_smtp == '1'){
        $SmtpServer = 'SMTP SERVER';
        $SmtpPort = 'SMTP PORT';
        $SmtpUser = 'SMTP USER';
        $SmtpPass = 'SMTP PASSWORD';
        $to = $emailto;
        $SMTPMail = new SMTPClient ($SmtpServer, $SmtpPort, $SmtpUser, $SmtpPass, $from, $to, $subject, $body);
        $SMTPChat = $SMTPMail->SendMail();
        $response = $SMTPChat ? $response_sent : $response_error;
    } else {
        // Create header
        $headers = "From: $from'r'n";
        $headers .= "MIME-Version: 1.0'r'n";
        $headers .= "Content-type: text/plain; charset=utf-8'r'n";
        $headers .= "Content-Transfer-Encoding: quoted-printable'r'n";
        // Send email
        $mail_sent = @mail($emailto, $subject, $body, $headers);
        $response = $mail_sent ? $response_sent : $response_error;
    }
    return $response;
}
// Remove any un-safe values to prevent email injection
function filter($value) {
    $pattern = array("/'n/", "/'r/", "/content-type:/i", "/to:/i", "/from:/i", "/cc:/i");
    $value = preg_replace($pattern, "", $value);
    return $value;
}
exit;
?>

在 sendEmail() 函数中,您可以删除此行:

$response_sent = 'Thank you. Your messsage has been received.';

然后更改这 2 行:

$response = $SMTPChat ? $response_sent : $response_error;
//Becomes:
$response = $SMTPChat ? true : $response_error;
$response = $mail_sent ? $response_sent : $response_error;
//Becomes:
$response = $mail_sent ? true : $response_error;

这样如果一切正常,return $response;将返回 TRUE

然后将echo $response;更改为类似

if($response===true){
  $url="thankyou.php"
  if(!headers_sent()){
    header("location:".$url);
  }else{
    echo "<script type='text/javascript'>document.location='".$url."';</script>";
  }
  exit();
}else{
  echo $response;
}

解释:我们将使用 $response_sent 来检查一切是否成功。如果 $response_sent 是绝对 TRUE(三重 =),我们告诉脚本重定向用户,否则我们会像现在一样回显错误消息。