2013-10-09 107 views
1

我目前正在使用PHP表單處理程序發送帶有表單字段的電子郵件。我想轉換此處理器,以便在成功提交後它會將您重定向到不同的頁面,而不是回顯成功消息(就像當前那樣)。以下是我用於處理器的代碼。提前致謝!表單提交重定向而不是回顯成功消息?

<?php 
if(isset($_POST['email'])) { 

    // EDIT THE 2 LINES BELOW AS REQUIRED 
    $email_to = "[email protected]"; 
    $email_subject = "Email Subject Goes Here"; 


    $organization_name_field = $_POST['organization']; //required 
    $contact_name_field = $_POST['name'];  //required 
    $city_field = $_POST['city'];   //not required 
    $state_field = $_POST['state'];   //not required 
    $zipcode_field = $_POST['zip'];   //not required 
    $email_field = $_POST['email'];   //not required 
    $phone_number_field = $_POST['phone'];  //not required 
    $email_message = "Form details below:\n\n"; 

    function clean_string($string) { 
     $bad = array("content-type","bcc:","to:","cc:","href"); 
     return str_replace($bad,"",$string); 
    } 

    $email_message .= "Organization Name: ".clean_string($organization_name_field)."\n"; 
    $email_message .= "Contact Name: ".clean_string($contact_name_field)."\n"; 
    $email_message .= "City: ".clean_string($city_field)."\n"; 
    $email_message .= "State: ".clean_string($state_field)."\n"; 
    $email_message .= "Zip Code: ".clean_string($zipcode_field)."\n"; 
    $email_message .= "Email: ".clean_string($email_field)."\n"; 
    $email_message .= "Telephone: ".clean_string($phone_number_field)."\n"; 


// create email headers 
$headers = 'From: '.$contact_name_field."\r\n". 
'Reply-To: '.$email_field."\r\n" . 
'X-Mailer: PHP/' . phpversion(); 
@mail($email_to, $email_subject, $email_message, $headers); 
?> 

<!-- include your own success html here --> 

Thank you for contacting us. We will be in touch with you very soon. 

<?php 
} 
?> 
+3

只需添加'header('Location:someotherpage.php');'而不是謝謝... – cmorrissey

+0

千萬不要使用那些錯誤抑制'@'s!請檢查郵件功能的狀態以查看郵件是否被正確發送(到SMTP服務器)。 –

+0

電子郵件通過正確的,我不能讓表單重定向到感謝頁面。 – APAD1

回答

3

替換以下:

?> 

<!-- include your own success html here --> 

Thank you for contacting us. We will be in touch with you very soon. 

<?php 
} 
?> 

有了:

//Redirect user to another page 
header('Location: email-success.php'); //Replace email-success.php with the page you want them to be redirected to! 
} 
?> 
+1

這工作完美,非常感謝你! – APAD1

-1
<?php 
if(isset($_POST['email'])) { 

    // EDIT THE 2 LINES BELOW AS REQUIRED 
    $email_to = "[email protected]"; 
    $email_subject = "Email Subject Goes Here"; 


    $organization_name_field = $_POST['organization']; //required 
    $contact_name_field = $_POST['name'];  //required 
    $city_field = $_POST['city'];   //not required 
    $state_field = $_POST['state'];   //not required 
    $zipcode_field = $_POST['zip'];   //not required 
    $email_field = $_POST['email'];   //not required 
    $phone_number_field = $_POST['phone'];  //not required 
    $email_message = "Form details below:\n\n"; 

    function clean_string($string) { 
     $bad = array("content-type","bcc:","to:","cc:","href"); 
     return str_replace($bad,"",$string); 
    } 

    $email_message .= "Organization Name: ".clean_string($organization_name_field)."\n"; 
    $email_message .= "Contact Name: ".clean_string($contact_name_field)."\n"; 
    $email_message .= "City: ".clean_string($city_field)."\n"; 
    $email_message .= "State: ".clean_string($state_field)."\n"; 
    $email_message .= "Zip Code: ".clean_string($zipcode_field)."\n"; 
    $email_message .= "Email: ".clean_string($email_field)."\n"; 
    $email_message .= "Telephone: ".clean_string($phone_number_field)."\n"; 


// create email headers 
$headers = 'From: '.$contact_name_field."\r\n". 
'Reply-To: '.$email_field."\r\n" . 
'X-Mailer: PHP/' . phpversion(); 
@mail($email_to, $email_subject, $email_message, $headers); 
?> 

<!-- include your own success html here --> 

<?php 
header('Location: yoursuccesspage.php'); 
?> 

<?php 
} 
?> 

見,當你只需要頭到一個新的頁面更迭。

+0

但也檢查'mail'的返回值。 –

+0

謝謝,這就是我想我應該做的,但是用這樣的代碼,它只是掛在process.php,它只是一個空白的白頁。電子郵件確實正確無誤,它只是不會重定向到感謝頁面。有任何想法嗎? – APAD1

+2

這實際上不會起作用,因爲在調用header()之前,您將向瀏覽器輸出空白字符,以查看上面@StevenFarley的回答以獲得正確的實現。 –

相關問題