如何在呈现一些HTML后重定向PHP?

时间:2014-04-27 16:26:59

标签: php

我知道之前已经问过这个问题,人们回答header(Location: "link.html");这样的事情很棒!但我不知道放在哪里,相信我,我试过了!

基本上,我有一个非常简单的PHP代码,用于发送已输入表单的信息。代码如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Untitled 1</title>
</head>

<body>
<?php
$to='myemail@address.com';
$subject='email subject line';
$qanswer=$_POST['qanswer'];

$FirstName=$_POST['FirstName'];
$Surname=$_POST['Surname'];
$CustomerEmail=$_POST['CustomerEmail'];
$CustomerTelephone=$_POST['CustomerTelephone'];
$PickupLocation=$_POST['PickupLocation'];
$AddressLine1=$_POST['AddressLine1'];
$AddressLine2=$_POST['AddressLine2'];
$TownCity=$_POST['TownCity'];
$County=$_POST['County'];
$Postcode=$_POST['Postcode'];
$Destination=$_POST['Destination'];
$PickupDate=$_POST['PickupDate'];
$PickupTime=$_POST['PickupTime'];
$message="FirstName:  ".$FirstName. "\r\n" . "Surname:  ".$Surname. "\r\n" .     "CustomerEmail:  ".$CustomerEmail. "\r\n" . "CustomerTelephone:  ".$CustomerTelephone.     "\r\n" . "PickupLocation:  ".$PickupLocation. "\r\n" . "AddressLine1:  ".$AddressLine1.     "\r\n" . "AddressLine2:  ".$AddressLine2. "\r\n" . "TownCity:  ".$TownCity. "\r\n" .     "County:  ".$County. "\r\n" . "Postcode:  ".$Postcode. "\r\n" . "Destination:      ".$Destination. "\r\n" . "PickupDate: ".$PickupDate. "\r\n" . "PickupTime: ".$PickupTime;

if ($qanswer==10)
{
mail($to,$subject,$message);
echo 'Thank You! Someone will be in contact shortly to confirm your booking';
}
else
{
echo 'Your booking failed to send! Please ensure you answered the anti-spam question     correctly';
}

?>

</body>

</html>

我想要做的是在表单发送成功后,而不是显示一个空白页面,其中包含“谢谢!有人会很快联系以确认您的预订”我想重定向到名为“thankyoupage.html”的页面,位于同一目录中。同样,如果失败,我希望它重定向到名为“formsendfailure.html”的页面。

3 个答案:

答案 0 :(得分:3)

如果此页面仅用于发送邮件,请执行以下操作:

  • 删除所有HTML,因此您最终只能使用<?php ..?>标记内的PHP。
  • 然后您可以自由使用header('Location: url.php'); - 问题可能是您已经发送了输出,并且在这些情况下您无法设置标头。这是因为发送真实内容迫使PHP按原样完成标题,并且在内容之后无法发送标题。
  • 然后我会在查询字符串中发回成功/错误条件,而不是使用echo。因此,如果一切顺利,您可以重定向到/your/url.php?result=ok,如果没有,则可以/your/url.php?result=error。在目标页面中,您需要检测到这一点并呈现成功消息(“ok”),失败消息(“错误”)或任何内容。
  • 值得添加支票以确保您实际上进行后期操作 - 好奇的人或饥饿的搜索引擎仍然可以访问该网址,并且您不希望输入任何错误消息。

你可以在会话变量中做最后一步,但这有点复杂,而且对于这个简单的要求可能有点多了。

答案 1 :(得分:1)

Halfer说的很多。标题必须是您发送的第一件事,即您不能在它之前输出任何其他内容,否则它将失败。

最好的办法是在if - else设置一个值,如下所示:

if ($qanswer==10)
{
mail($to,$subject,$message);
$redirect = 0;
//echo 'Thank You! Someone will be in contact shortly to confirm your booking';
}
else
{
$redirect = 1;
//echo 'Your booking failed to send! Please ensure you answered the anti-spam question     correctly';
}

然后在文档的顶部,第一件事应该是:

<?php
    if($redirect==0){
        header("Location: success.php");
    }else{
        header("Location: fail.php");
    }
?>

然后让每个页面显示自己的消息,即“您的预订成功”。

答案 2 :(得分:0)

您必须将表单处理移动到php文档的顶部。您必须在php输出其他内容之前发出标题重定向。

目前,在解析php之前,第一个<?php标记之前的所有内容都将输出到浏览器。

相关问题