2017-08-10 20 views
0

此PHP脚本会将用户重定向到一个白页,其中声明“感谢您与我们联系,我们将很快与您联系。”我希望找到一种方法将其重定向到不同的位置,例如google.com或我的主页。我在同一行中放置了一个HTML文件,其中显示“感谢您与我们联系,我们会尽快与您联系”,但这不起作用。我目前的PHP脚本重定向到白页。如何告诉我的脚本重定向到其他位置?

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

    // EDIT THE 2 LINES BELOW AS REQUIRED 
    $email_to = ""; 
    $email_subject = "Your email subject line"; 

    function died($error) { 
     // your error code can go here 
     echo "We are very sorry, but there were error(s) found with the form you submitted. "; 
     echo "These errors appear below.<br /><br />"; 
     echo $error."<br /><br />"; 
     echo "Please go back and fix these errors.<br /><br />"; 
     die(); 
    } 


    // validation expected data exists 
    if(!isset($_POST['first_name']) || 
     !isset($_POST['last_name']) || 
     !isset($_POST['email']) || 
     !isset($_POST['telephone']) || 
     !isset($_POST['comments'])) { 
     died('We are sorry, but there appears to be a problem with the form you submitted.');  
    } 



    $first_name = $_POST['first_name']; // required 
    $last_name = $_POST['last_name']; // required 
    $email_from = $_POST['email']; // required 
    $telephone = $_POST['telephone']; // not required 
    $comments = $_POST['comments']; // required 

    $error_message = ""; 
    $email_exp = '/^[A-Za-z0-9._%-][email protected][A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; 

    if(!preg_match($email_exp,$email_from)) { 
    $error_message .= 'The Email Address you entered does not appear to be valid.<br />'; 
    } 

    $string_exp = "/^[A-Za-z .'-]+$/"; 

    if(!preg_match($string_exp,$first_name)) { 
    $error_message .= 'The First Name you entered does not appear to be valid.<br />'; 
    } 

    if(!preg_match($string_exp,$last_name)) { 
    $error_message .= 'The Last Name you entered does not appear to be valid.<br />'; 
    } 

    if(strlen($comments) < 2) { 
    $error_message .= 'The Comments you entered do not appear to be valid.<br />'; 
    } 

    if(strlen($error_message) > 0) { 
    died($error_message); 
    } 

    $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 .= "First Name: ".clean_string($first_name)."\n"; 
    $email_message .= "Last Name: ".clean_string($last_name)."\n"; 
    $email_message .= "Email: ".clean_string($email_from)."\n"; 
    $email_message .= "Telephone: ".clean_string($telephone)."\n"; 
    $email_message .= "Comments: ".clean_string($comments)."\n"; 

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

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



<?php 

} 
?> 
+7

使用PHP函数'header(“location:yourLink.php”);' – yoeunes

+0

旁注,您的电子邮件验证和名称验证将绊倒一些合法的nmes \电子邮件地址 – rtfm

+0

小心电子邮件注入! – perodriguezl

回答

0

这基本上是这个问题的一个副本: How to make a redirect in PHP?

总之,你需要使用头功能重定向到您想要的页面。

<?php 
    header('Location: http://www.yoursite.com/thank_you_page.html') ; 
?> 

确保在调用标题函数之前没有其他文本被打印,否则它将不起作用。 (如果你的任何错误引发例如)

+0

请提问对应的评论。谢谢! – localheinz

+0

我没有优惠特权。 –