Forum Moderators: coopster

Message Too Old, No Replies

header warning when mailing form data

tried various experiments

         

coleus

4:17 pm on Aug 31, 2006 (gmt 0)

10+ Year Member



Hello, this is my first post. Basically, I want to get form data, [validate it-later!], display a thank you page if everything was ok, and email the data. For ease of testing, I stripped out the validation code; I'll deal with that after I get the basics working.
Problem: Either the mail gets sent or the confirmation appears, but I can't seem to get both. I've tried many variations.

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?

eelixduppy

4:30 pm on Aug 31, 2006 (gmt 0)




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...]

eelixduppy

5:42 pm on Aug 31, 2006 (gmt 0)



Forgive me; I forgot to welcome you to the greatest site on the net!

Welcome to Webmaster World coleus! I'm sure you will find this community as informative as the rest of us find it ;)

coleus

5:59 pm on Sep 1, 2006 (gmt 0)

10+ Year Member



Thanks, eelixduppy, for your prompt response and for the welcome, too. I was a little shy about posting. I'm new at PHP and server-side scripting, so still on the learning curve.

I'm following up on the output buffering information and will be trying it out. Thanks, again.