在我们正确提交表单之后,我正在尝试将人们重定向到PDF,在我这样做之前,我会对表单字段进行一些检查以使它们已经正确填写,现在我正在尝试使用header()做我的重定向,但是因为我在收到错误之前已经多次回应。以下是我的代码,我该怎么办?
<?php
if(isset($_POST['submit'])) {
if($valid_fname == "Y") {
if($valid_sname == "Y") {
if($valid_company == "Y") {
if($valid_email == "Y") {
}
else {
echo "<p class=\"secText\">Please enter a valid email address</p>\n";
}
}
else{
echo "<p class=\"secText\">Please enter the company you work for</p>\n";
}
}
else {
echo "<p class=\"secText\">Please enter your surname</p>\n";
}
}
else {
echo "<p class=\"secText\">Please enter your first name</p>\n";
}
if(($valid_fname == "Y")&&($valid_sname == "Y")&&($valid_company == "Y")&&($valid_email == "Y")) {
echo "<p class=\"secText\">Thank you for confirming your details, you will be re-directed to \"The Personal Touch\" Whitepaper shortly.</p>\n";
header('Location: http://www.sefasinnovation.co.uk/personal_touch.pdf');
exit();
}
}
?>
编辑好的,我最终使用了一些javascript
if(($valid_fname == "Y")&&($valid_sname == "Y")&&($valid_company == "Y")&&($valid_email == "Y")) {
echo "<p class=\"secText\">Thank you for confirming your details, you will be re-directed to \"The Personal Touch\" Whitepaper shortly.</p>\n";
echo "<script type=\"text/javascript\">\n";
echo " <!--\n";
echo " setTimeout(\"window.location = 'http://www.sefasinnovation.co.uk/personal_touch.pdf';\",5000);\n";
echo "//-->\n";
echo "</script>\n";
}
答案 0 :(得分:1)
尝试建立一个链接,将用户重定向到pdf页面,以便该链接包含pdf页面网址
<a href="http://www.sefasinnovation.co.uk/personal_touch.pdf">Thank you for confirming your details </a>
或者您可以使用元标记在2秒后重定向用户
<meta http-equiv="refresh" content="2; url=http://www.sefasinnovation.co.uk/personal_touch.pdf" />
答案 1 :(得分:1)
您可以使用output buffering仅在发送标题后发送内容。
if(($valid_fname == "Y")&&($valid_sname == "Y")&&($valid_company == "Y")&&($valid_email == "Y")) {
ob_start();
echo "<p class=\"secText\">Thank you for confirming your details, you will be re-directed to \"The Personal Touch\" Whitepaper shortly.</p>\n";
header('Location: http://www.sefasinnovation.co.uk/personal_touch.pdf');
ob_end_flush();
exit();
}
然而,由于在重定向完成之前几乎没有时间显示消息,这种方法将毫无用处。
这里最好的方法可能是将您的网页重定向到一个小的HTML页面,该页面会在经过一段时间后触发JavaScript重定向。这是一般的想法。你应该对细节进行排序。
<强> PHP 强>
if(($valid_fname == "Y")&&($valid_sname == "Y")&&($valid_company == "Y")&&($valid_email == "Y")) {
header('Location: http://www.sefasinnovation.co.uk/notification.html');
exit();
// You could also avoid redirection to an HTML file and output the code directly
echo <<<HTML
<html>
<head>
<title>Enter desired title</title>
<script type="text/javascript">
setTimeout(function(){
window.location = "http://www.sefasinnovation.co.uk/personal_touch.pdf";
}, 5000);
</script>
</head>
<body>
<p class="secText">Thank you for confirming your details, you will be re-directed to "The Personal Touch" Whitepaper shortly.</p>
<p>If this page isn't redirected in 5 seconds please click <a href="http://www.sefasinnovation.co.uk/personal_touch.pdf">here</a>.</p>
</body>
</html>
HTML;
}
notification.html (上面的PHP代码也可以吐出此代码,但前提是页面上没有输出)
<html>
<head>
<title>Enter desired title</title>
<script type="text/javascript">
setTimeout(function(){
window.location = "http://www.sefasinnovation.co.uk/personal_touch.pdf";
}, 5000);
</script>
</head>
<body>
<p class="secText">Thank you for confirming your details, you will be re-directed to "The Personal Touch" Whitepaper shortly.</p>
<p>If this page isn't redirected in 5 seconds please click <a href="http://www.sefasinnovation.co.uk/personal_touch.pdf">here</a>.</p>
</body>
</html>
notification.html中的附加链接应允许用户在禁用JavaScript时进行手动重定向。
答案 2 :(得分:0)
你的问题在这里:
if(($valid_fname == "Y")&&($valid_sname == "Y")&&($valid_company == "Y")&&($valid_email == "Y")) {
echo "<p class=\"secText\">Thank you for confirming your details, you will be re-directed to \"The Personal Touch\" Whitepaper shortly.</p>\n";
header('Location: http://www.sefasinnovation.co.uk/personal_touch.pdf');
exit();
}
如果要发送重定向标头,则无法向页面输出任何内容。删除echo
语句并再次尝试该页面。
或者,您可以编写带有重定向<meta>
标记的HTML页面(您可以将其延迟5秒钟然后重定向。您的消息将起作用)。
在Google上查找教程。