2013-10-09 75 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的回答以获得正确的实现。 –

相关问题