Forum Moderators: coopster
Using the following code, I get the warning "Cannot modify header information - headers already sent .... The line it points to IS the line where I'm using the location header redirect.
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Form Test</title>
</head>
<body>
<?php
error_reporting(E_ALL);
// initialize data fields
$nameFirst="";
$nameLast="";
$phone="";
$email="";
$submit="";
extract($_POST, EXTR_IF_EXISTS);
// variables for mailing
$mailto = "me@mine.com";
$mailsubj = "adhoc Form Testing";
$mailhead = "From: $email\n";
$mailbody="";
// loop through the $_POST array
foreach ($_POST as $key=>$value) {
$mailbody.=$key.": " . $value . "\n";
}
// send mail, if submit button was used
if (isset($_POST['submit'])) {
mail($mailto,$mailsubj,$mailbody,$mailhead);
header("Location: http:/www.mine.com/thanks.html");
}
?>
</body>
</html>
In a previous version, I removed the $mailhead variable, but got the same warning.
I am not knowingly echoing anything to the browser prior to the redirect. (But I probably will when I start adding validation subroutines). Can anyone help me understand and work around this issue?
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.
You must either remove the HTML at the beginning of the file or use a buffer.
As of PHP 4, you can use output buffering to get around this problem, with the overhead of all of your output to the browser being buffered in the server until you send it. You can do this by calling ob_start() [us2.php.net] and ob_end_flush() [us2.php.net] in your script, or setting the output_buffering configuration directive on in your php.ini or server configuration files.
Refer to the header documentation [us2.php.net] for more information.
Good luck!
All quotes from [php.net...]
Welcome to Webmaster World coleus! I'm sure you will find this community as informative as the rest of us find it ;)