PDA

View Full Version : PHP mail form tutorial trouble



exorxist
November 4th, 2004, 03:00 PM
I've been working with the php mail form tutorial, and everything is working great except for one thing. If I chose to not redirect, it works, but if I want to redirect the browser to another page, it gives me an error. This is the code

<?PHP
$to = "adress@wherever.com";

$subject = "Test Form Regus";

$headers = "From: Performance Bridge";

$forward = 0; <- when I change this to 1, it gives me an error

$location = "http://www.wherever.com";

$date = date ("l, F jS, Y");
$time = date ("h:i A");
$msg = "Below is the result of your feedback form. It was submitted on $date at $time.\n\n";
if ($_SERVER['REQUEST_METHOD'] == "POST") {
foreach ($_POST as $key => $value) {
$msg .= ucfirst ($key) ." : ". $value . "\n";
}
}
else {
foreach ($_GET as $key => $value) {
$msg .= ucfirst ($key) ." : ". $value . "\n";
}
}
mail($to, $subject, $msg, $headers);
if ($forward == 1) {
header ("Location:$location");
}
else {
echo "Thank you for submitting our form. We will get back to you as soon as possible.";
}

?>


Any ideas why

($forward == 1) {
header ("Location:$location");
}

will not let me redirect? Thanks in advance!

andr.in
November 4th, 2004, 03:31 PM
I suppose the error is something like "headers already sent"?
you either have to put that php code in the very beginning of the .php file or you have to use something else like
echo '<script type="text/javascript">location.href = "foo.php";</script>';
but that way you can use the back button to go back to the mail-sending script

exorxist
November 4th, 2004, 03:37 PM
Thanks Syko, that did it! Thanks so much.