如何在渲染了一些HTML之后重定向PHP


How to redirect a PHP after having rendered some HTML?

我知道以前有人问过这个问题,人们用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>

我想做的是当表格成功发送后,而不是显示一个空白页面,上面写着"谢谢!很快就会有人联系确认您的预订"。我想重定向到同一目录中的一个名为"谢谢page.html"的页面。同样,如果它失败了,我希望它重定向到一个名为"formsendfailure.html"的页面。

如果这个页面只是为了发送邮件,那么请执行以下操作:

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

您可以在会话变量中执行最后一步,但这有点复杂,而且对于这个简单的需求来说可能有点多。

差不多就是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");
    }
?>

然后让每个页面都显示自己的信息,即"您的预订成功了"。

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

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