我有一个PHP表单邮件程序脚本,它返回一个文本消息给浏览器。需要.php代码才能重定向到html页面

时间:2015-08-03 00:13:51

标签: php html redirect contact-form

这是HTML PARTIAL CODE ....

  <input type="hidden" name="recipient   value="msheree@msascodesignsolutions.com">
  <input type="hidden" name="subject" value="Submission" /> 
  <input type="hidden" name="redirect" value="thankyou.html" />
  <input type="hidden" name="projectinquiries" value="projectinquiries">
  <div class="btn-group">
  <a class="btn btn-default" href="mailto:email@emailaddress.com?Subject=Hello, %20Interested%20In%20A%20Quote" target="_top" role="button">Contact</a>
  <input class="btn btn-default" type="reset" value="Reset">
  <input class="btn btn-default" type="submit" value="Submit">
  …..

这是.php文件。它工作正常。但我不想在浏览器中显示短信。我想重定向到名为&#34; thankyou.html&#34;的.html页面。我改变了网络托管公司以节省资金。从godaddy.com到hostgator.com和hostgator无法迁移我的文件并使表单正常工作。我不知道.php。请帮帮我。

<?php

if(isset($_POST['email'])) {

// EDIT THE 2 LINES BELOW AS REQUIRED

$email_to = "msheree@msascodesignsolutions.com";

$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._%-]+@[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); 

?>

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

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

<!-- The above message displays in the browser. Please tell me what code and  exactly where to place it to redirect to a page called "thankyou.html" instead of getting the below message in the browser -->

<?php

}

?>

我不希望看到上述消息,而是改为重定向到html页面。

2 个答案:

答案 0 :(得分:2)

既然你提到你不懂PHP,我会在这里省略一些技术细节。

将它放在PHP标记的PHP脚本末尾,将URL替换为您要链接到的URL:

header('Location: http://www.msascodesignsolutions.com/thankyou.html');

所以最后几行应该是这样的:

// 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); 
header('Location: http://www.msascodesignsolutions.com/thankyou.html');

?>

这是最好,最干净的解决方案。根据您提供的PHP脚本,我认为没有理由这会失败。但是,如果由于某种原因你得到这样的信息:

  

警告:无法修改标头信息 - 已发送的标头...

您可以考虑在HTML中嵌入Javascript中创建重定向,如下所示:

<!-- include your own success html here -->
<script type="text/javascript">
    window.location = "http://www.msascodesignsolutions.com/thankyou.html"
</script>

这是一个比仅使用PHP的解决方案更糟糕的解决方案,但它将使您不必对PHP进行更重大的更改。

答案 1 :(得分:0)

我通常重定向的方式是添加一个$ redirect变量,然后我可以在脚本运行结束时调用它。例如:在我添加的页面或脚本的最顶部。

public class EmptyCollectionException extends RuntimeException {

    public EmptyCollectionException(String collection) {
        super("The " + collection + " is empty.");
    }
}

这必须是第一个运行的东西,否则会产生错误就是我所经历的。

然后我运行脚本,在脚本结束时,我会在关闭查询之前调用以下变量。我只是将“thankyou”页面文件名添加到顶部提交的标题函数中。

$redirect = header('Location: fileNameOfPageYouWantToRedirectTo.php');

然而,这将不允许您添加诸如“表单已提交”的消息,因为它将在消息在脚本中出现之前重定向,或者更好地说如果消息已创建,则重定向速度太快以至于访问者无法看到它(完全跳过),因为你重定向到感谢页面,你已经设置好了,不用担心Javascript。如果要使用多个表单的公共文件进行提交,并根据当前指向要填写的表单的链接更改重定向链接,也可以选择动态生成重定向链接。我认为如果你想重用它会更有效率。