dqvtm82066 2015-06-16 15:39 采纳率: 0%
浏览 23
已采纳

too long

Currently my contact form responds with a message. I would like to change it so it redirects to a thank you page instead. Can someone help me adjust my code accordingly?

I know I either need to do window.location or headerLocation but I'm not sure where to insert it in this particular template to make it work.

<?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: 
";
                        foreach ( $value as $two_dim_value )
                        $message .= "...$two_dim_value<br>";
                    }else {
                        $message .= $value != '' ? "$key: $value
" : '';
                    }
                }
            }

        $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."
$ip
$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
";
        $headers .= "MIME-Version: 1.0
";
        $headers .= "Content-type: text/plain; charset=utf-8
";
        $headers .= "Content-Transfer-Encoding: quoted-printable
";

        // 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("/
/", "//", "/content-type:/i", "/to:/i", "/from:/i", "/cc:/i");
    $value = preg_replace($pattern, "", $value);
    return $value;
}

exit;

?>
  • 写回答

1条回答 默认 最新

  • dongliyan9190 2015-06-16 15:57
    关注

    in your sendEmail() function you can remove this line:

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

    Then change those 2 lines:

    $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;
    

    So that return $response; will return TRUE if everything was fine

    And then change echo $response; To something like

    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;
    }
    

    Explaination: We will use $response_sent to check if everything was successful or not. If $response_sent is an absolute TRUE (triple =) we tell the script to redirect the user, or ELSE we echo the error message just like it is right now.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号