Reputation: 5855
I'm trying to redirect people to a PDF after they've submitted the form correctly, before I do this I do some checking of the form fields to make they've been filled out correctly, now I was trying to use header() to do my re-direct, but because I've echoed a number of times before I get an error. Here's my code below, what can I do?
<?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();
}
}
?>
EDIT Ok I got it to work with a bit of javascript in the end
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";
}
Upvotes: 1
Views: 1454
Reputation:
You could use output buffering to send content only after the headers have been sent.
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();
}
However since there is practically no time to display the message before the redirection is done this approach would be useless.
Your best approach here would probably be to redirect your page to a small HTML page which will trigger a JavaScript redirect after a certain amount of time has passed. Here is the general idea. You should sort the details out.
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 (the PHP code above could also spit this code out but only if there was not output on the page previously)
<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>
The additional link in notification.html should allow users to do a manual redirection in case JavaScript is disabled.
Upvotes: 1
Reputation: 298582
Your problem is here:
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();
}
You can't output anything to the page if you want to send redirection headers. Remove that echo
statement and try the page again.
Alternatively, you can write a HTML page with a redirection <meta>
tag (you can make it delay like 5 seconds and then redirect. Your message will then work).
Look it up on Google for a tutorial.
Upvotes: 0
Reputation: 13327
try to make a link to redirect the user to the pdf page so the link contains the pdf page url
<a href="http://www.sefasinnovation.co.uk/personal_touch.pdf">Thank you for confirming your details </a>
or you can use the meta tag to redirect the user after 2 second
<meta http-equiv="refresh" content="2; url=http://www.sefasinnovation.co.uk/personal_touch.pdf" />
Upvotes: 1