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";
}
You could use output buffering to send content only after the headers have been sent.
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
notification.html (the PHP code above could also spit this code out but only if there was not output on the page previously)
The additional link in notification.html should allow users to do a manual redirection in case JavaScript is disabled.