2015-03-31 61 views
-1
<?php 
if($_POST) 
{ 
    $to_email  = " [email protected]"; //Recipient email, Replace with own email here 

    //check if its an ajax request, exit if not 
    if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) AND strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') { 

     $output = json_encode(array(//create JSON data 
      'type'=>'error', 
      'text' => 'Sorry Request must be Ajax POST' 
     )); 
     die($output); //exit script outputting json data 
    } 

    //Sanitize input data using PHP filter_var(). 
    $user_name  = filter_var($_POST["user_name"], FILTER_SANITIZE_STRING); 
    $user_email  = filter_var($_POST["user_email"], FILTER_SANITIZE_EMAIL); 
    $country_code = filter_var($_POST["country_code"], FILTER_SANITIZE_NUMBER_INT); 
    $phone_number = filter_var($_POST["phone_number"], FILTER_SANITIZE_NUMBER_INT); 
    $subject  = filter_var($_POST["subject"], FILTER_SANITIZE_STRING); 
    $message  = filter_var($_POST["msg"], FILTER_SANITIZE_STRING); 

    //additional php validation 
    if(strlen($user_name)<4){ // If length is less than 4 it will output JSON error. 
     $output = json_encode(array('type'=>'error', 'text' => 'Name is too short or empty!')); 
     die($output); 
    } 
    if(!filter_var($user_email, FILTER_VALIDATE_EMAIL)){ //email validation 
     $output = json_encode(array('type'=>'error', 'text' => 'Please enter a valid email!')); 
     die($output); 
    } 
    if(!filter_var($country_code, FILTER_VALIDATE_INT)){ //check for valid numbers in country code field 
     $output = json_encode(array('type'=>'error', 'text' => 'Enter only digits in country code')); 
     die($output); 
    } 
    if(!filter_var($phone_number, FILTER_SANITIZE_NUMBER_FLOAT)){ //check for valid numbers in phone number field 
     $output = json_encode(array('type'=>'error', 'text' => 'Enter only digits in phone number')); 
     die($output); 
    } 
    if(strlen($subject)<3){ //check emtpy subject 
     $output = json_encode(array('type'=>'error', 'text' => 'Subject is required')); 
     die($output); 
    } 
    if(strlen($message)<3){ //check emtpy message 
     $output = json_encode(array('type'=>'error', 'text' => 'Too short message! Please enter something.')); 
     die($output); 
    } 

    //email body 
    $message_body = "\r\nRegarding : ".$subject."\r\n\r\n-".$message."\r\n\r\n-".$user_name."\r\nEmail : ".$user_email."\r\nPhone Number : (".$country_code.") ". $phone_number ; 

    //proceed with PHP email. 
    $headers = 'From: '.$user_name.'' . "\r\n" . 
    'Reply-To: '.$user_email.'' . "\r\n" . 
    'X-Mailer: PHP/' . phpversion(); 

    $send_mail = mail($to_email, $subject, $message_body, $headers); 

    if(!$send_mail) 
    { 
     //If mail couldn't be sent output error. Check your PHP email configuration (if it ever happens) 
     $output = json_encode(array('type'=>'error', 'text' => 'Could not send mail! Please check your PHP mail configuration.')); 
     die($output); 
    }else{ 
     $output = json_encode(array('type'=>'message', 'text' => 'Hi '.$user_name .' Thank you for your email')); 
     die($output); 
    } 
} 
?> 

當我提交表單時,它顯示的是感謝您的郵件,但我想重定向以便在提交後感謝您。將此感謝信息轉換爲重定向謝謝頁面

回答

0

創建thank_you.php頁面,在你展示你else條件謝謝消息給頭位置

thank_you.php?msg='thank you message' 

這樣和感謝頁面

0

您可以重複此消息使用header()或重定向功能
header("Location: http://mydomainname.com/thankyou.php"); die();

OR

redirect('thankyou.php');

+0

我使用根據你的代碼,但它不起作用。 – andy 2015-03-31 06:32:02

+0

if(!$ send_mail) \t { \t \t //如果郵件無法發送輸出錯誤。檢查您的PHP電子郵件配置(如果它發生) \t \t $ output = json_encode(array('type'=>'error','text'=>'無法發送郵件!請檢查您的PHP郵件配置。') ); \t \t die($ output);其他{ \t \t header(「Location:http://www.xyz.com.au/contact-form-thank-you.php」); die(); \t} – andy 2015-03-31 06:32:37

+0

@andy如何重定向 – Ghostman 2015-03-31 06:33:13