redirect to thankyou page failure

Hi can anyone advise me on where I am going wrong! when I submit my form I get this message >
Warning: Cannot modify header information - headers already sent by (output started at /home/hoteldirect/public_html/mailer.php:1) in /home/hoteldirect/public_html/mailer.php on line 35

Line 35 is on my php form is >
/* Redirect visitor to the thank you page */
header(‘Location: thankyou.html’);
exit();

This is my php form in full >

<?php $to = "[email protected]"; /* Check all form inputs using check_input function */ $name = check_input($_POST['name'], "Enter your name"); $email = check_input($_POST['email']); $tel = check_input($_POST['tel'], "Enter your Telephone number"); $business = check_input($_POST['business'], "Enter your business name"); $captcha = check_input($_POST['captcha']); /* If e-mail is not valid show error message */ if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) { show_error("Email address not valid"); } if (!preg_match("/5/", $captcha)) { show_error("Oops! please enter the correct maths number"); } /* Let's prepare the message for the e-mail */ $message = " Name: $name Email: $email Telephone number: $tel Business: $business "; /* Send the message using mail() function */ mail($to,$subject,$message,$headers," -f [email protected]"); /* Redirect visitor to the thank you page */ header('Location: thankyou.html'); exit(); /* Functions we used */ function check_input($data, $problem='') { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); if ($problem && strlen($data) == 0) { show_error($problem); } return $data; } function show_error($myError) { ?>

Please correct the following error:

<?php echo $myError; ?>

Hit the back button and try again

<?php
exit();
}
?>

And this is a suggested set up from my server >
To get this form to work with our hosting properly you will need to set the “From” address in the code as below:
Where the code says: mail($to,$subject,$message,$headers);

You need to add the from address in this format:
mail($to,$subject,$message,$headers,"-f [email protected]");

<?php // Pick up the form data and assign it to variables $name = $_POST['name']; $email = $_POST['email']; $topic = $_POST['topic']; $comments = $_POST['comments']; // Build the email (replace the address in the $to section with your own) $to = '[email protected]'; $subject = "New message: $topic"; $message = "$name said: $comments"; $headers = "From: $email"; // Send the mail using PHPs mail() function mail($to, $subject, $message, $headers); // Redirect header("Location: thankyou.html"); ?>

I am a newbie and the form works fine apart from redirecting to a custom thank you page, I have tried everything but still does not work! any advice to solve this problem would be greatly appreciated :smiley:
Thanks
Paul

header() needs to be sent before the headers, I’m guessing that you’re processing that after they have been sent.

You could use JS to redirect them, I put it in a function making it as simple as header():

[PHP]
function redirect($url)
{
$string = ‘’;

echo $string;

}

//Usage
redirect(‘thankyou.html’);
[/PHP]

Headers() needs to be called before anything appears in HTML - including whitespace!

You will need to do all processing with no output BEFORE you call headers.
If this presents a problem you could always use output buffering.

Place this at the very top of the file
[php]<?php ob_start(); ?>[/php]

and place this at the very end of the file:
[php]<?php ob_end_flush(); ?>[/php]

Hope that helps,
Red :wink:

Sponsor our Newsletter | Privacy Policy | Terms of Service