Hi I'm finding this site usefull. Thanks. I've just resolved an issue with my form and I think this is the last hurdle. My contact form works well and I get my thank you webpage displaying ok when all the fields are filled out, but if at least one field is left blank and then I submit I get this error: Warning: Cannot modify header information - headers already sent by (output started at /home/crikle/public_html/allthatmatters/mailer.php:31) in /home/crikle/public_html/allthatmatters/mailer.php on line 52 I think my header command is correct because I have used it with a different set of coding on another site, but it does'nt seem to like this set of coding I'm now using. (the header command is near the end of this code) Hope you can help: <?php if(isset($_POST['submit'])) { $to = "goodguy@btinternet.com"; $subject = "Form"; $name_field = $_POST['name']; $email_field = $_POST['email']; $message = $_POST['message']; if ($email_field == "") { echo "<h4>Please enter your email address.</h4>"; echo "<a href='javascript:history.back(1);'>BACK</a>"; } elseif(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email)) { echo "<h4>Your email address is invalid. Please try again.</h4>"; echo "<a href='javascript:history.back(1);'>BACK</a>"; } elseif ($name_field == "") { echo "<h4>Please enter your name.</h4>"; echo "<a href='javascript:history.back(1);'>BACK</a>"; } elseif ($message == "") { echo "<h4>Please enter your query.</h4>"; echo "<a href='javascript:history.back(1);'>BACK</a>"; } if( isset($_POST['check']) ) { foreach($_POST['check'] as $value) { $check_msg .= "Checked: $value\n"; } } $body = "From: $name_field\n E-Mail: $email_field\n $check_msg Message: $message\n"; { header( "Location: http://www.all-that-matters.com/thankyou.htm" ); } mail($to, $subject, $body); } else { echo "Failed"; } ?>
Add the below function immediately after the PHP start tag ob_start(); PHP: so will look something like <?php ob_start(); /* your code here! */ ?> PHP: You should always send the headers BEFORE you output anything to the client. And also wrap your PHP code inbetween php tags because it improves reability
Hello, The problem is it's trying to redirect when there has been output sent to the browser (error msg saying fill in all feilds, ect) which means the redirect can't be set. Try this: if(isset($_POST['submit'])) { $to = "goodguy@btinternet.com"; $subject = "Form"; $name_field = $_POST['name']; $email_field = $_POST['email']; $message = $_POST['message']; if ($email_field == "") { echo "<h4>Please enter your email address.</h4>"; echo "<a href='javascript:history.back(1);'>BACK</a>"; } elseif(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email)) { echo "<h4>Your email address is invalid. Please try again.</h4>"; echo "<a href='javascript:history.back(1);'>BACK</a>"; } elseif ($name_field == "") { echo "<h4>Please enter your name.</h4>"; echo "<a href='javascript:history.back(1);'>BACK</a>"; } elseif ($message == "") { echo "<h4>Please enter your query.</h4>"; echo "<a href='javascript:history.back(1);'>BACK</a>"; } else { if( isset($_POST['check']) ) { foreach($_POST['check'] as $value) { $check_msg .= "Checked: $value\n"; } } $body = "From: $name_field\n E-Mail: $email_field\n $check_msg Message: $message\n"; mail($to, $subject, $body); header( "Location: http://www.all-that-matters.com/thankyou.htm" ); } } else { echo "Failed"; } PHP: It only tries to redirect & send the email if there are no errors. Sky22
thanks it worked except....my form field validation code is now ineffective. I may need to re-locate your code? this is the validation code that is now ineffective: if ($email_field == "") { echo "<h4>Please enter your email address.</h4>"; echo "<a href='javascript:history.back(1);'>BACK</a>"; } elseif(!eregi("^[a-z0-9]+([_\\.-][a-z0-9]+)*" ."@"."([a-z0-9]+([\.-][a-z0-9]+)*)+"."\\.[a-z]{2,}"."$",$email)) { echo "<h4>Your email address is invalid. Please try again.</h4>"; echo "<a href='javascript:history.back(1);'>BACK</a>"; } elseif ($name_field == "") { echo "<h4>Please enter your name.</h4>"; echo "<a href='javascript:history.back(1);'>BACK</a>"; } elseif ($message == "") { echo "<h4>Please enter your query.</h4>"; echo "<a href='javascript:history.back(1);'>BACK</a>"; }
the reason the verification isn't working is because ob_start(); is caching the output (errors) and the redirect is sending the user to the new page before the cache (errors) is shown to the user. Sky22