Hello Dp members, I have created a php script for contact us form & script is as follows: <?php if(isset($_POST['sbtnSubmit'])) { $to = "webmaster@website.com"; $subject = "Email Subject"; $message = "Message \n\nName: ".$_POST['txtName']."\n\nE-mail Address: ".$_POST['txtEmail']."\n\nMessage: ".$_POST['taMessage']; $headers = 'From: website@No-Reply.com'."\r\n".'Reply-To: website@No-Reply.com'."\r\n".'X-Mailer: PHP/'.phpversion(); mail($to, $subject, $message, $headers); header('Location:thanks.html'); // This is line number 9 exit; } ?> PHP: I am getting emails when I submit this form but after submission I want to redirect my website to page "thanks.html" but by some reason its not working & I am facing follwing warning: Warning: Cannot modify header information - headers already sent by (output started at /home/content/o/d/x/odxfusion1/html/test/contact.php:1) in /home/content/o/d/x/odxfusion1/html/test/contact.php on line 9 I know, I am sending headers already but how I can redirect my website from "contact.php" page to "thanks.html" page without any error or warning. Here is the complete code for this form (HTML form + PHP) <?php if(isset($_POST['sbtnSubmit'])) { $to = "webmaster@website.com"; $subject = "Email Subject"; $message = "Message \n\nName: ".$_POST['txtName']."\n\nE-mail Address: ".$_POST['txtEmail']."\n\nMessage: ".$_POST['taMessage']; $headers = 'From: website@No-Reply.com'."\r\n".'Reply-To: website@No-Reply.com'."\r\n".'X-Mailer: PHP/'.phpversion(); mail($to, $subject, $message, $headers); header('Location:thanks.html'); // This is line number 9 exit; } ?> <form name="frmContact" action="contact.php" method="post"> <table> <tr> <td><sup>*</sup>Name:</td> <td width="245"><input type="text" name="txtName" id="txtName" value="" size="25" /></span></td> </tr> <tr> <td><sup>*</sup>Email:</td> <td><input type="text" name="txtEmail" id="txtEmail" value="" size="25" /></td> </tr> <tr> <td><sup>*</sup>Your Message:</td> <td><textarea name="taMessage" rows="5" cols="30"></textarea></td> </tr> <tr> <td></td> <td><input type="submit" name="sbtnSubmit" value="Submit" /></td> </tr> </table> </form> PHP: Any help will be appreciated. Thanks in advance, Jimmy
Cannot modify header information mean there is an text after the header page please make sure the header is send before any thing and use @mail($to, $subject, $message, $headers);
i mean <?php if(isset($_POST['sbtnSubmit'])) { $to = "webmaster@website.com"; $subject = "Email Subject"; $message = "Message \n\nName: ".$_POST['txtName']."\n\nE-mail Address: ".$_POST['txtEmail']."\n\nMessage: ".$_POST['taMessage']; $headers = 'From: '."\r\n".'Reply-To: '."\r\n".'X-Mailer: PHP/'.phpversion(); mail($to, $subject, $message, $headers); header('Location:thanks.html'); // This is line number 9 exit; } ?> must be the first thing in the page not in the form
The proper way of avoiding headers already sent error is to make sure 1. You're not using echo or print before the header function 2. There is no html or whitespace before the header function outside the php tags(you can use as much space inside <?php and ?>) If you can't avoid either of the above, use ob_start at the top of the page.