Headers already sent Error

Discussion in 'PHP' started by jimmy4feb, Jun 11, 2010.

  1. #1
    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
     
    Last edited: Jun 11, 2010
    jimmy4feb, Jun 11, 2010 IP
  2. ram4nd

    ram4nd Active Member

    Messages:
    167
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    50
    #2
    Try to put html in php's echo command. like: "echo 'your html here';"
     
    ram4nd, Jun 11, 2010 IP
  3. Scripts man

    Scripts man Guest

    Messages:
    51
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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);
     
    Scripts man, Jun 11, 2010 IP
  4. Scripts man

    Scripts man Guest

    Messages:
    51
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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
     
    Scripts man, Jun 11, 2010 IP
  5. jimmy4feb

    jimmy4feb Peon

    Messages:
    56
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Yes, My Php coding is on the top of the page & HTML is under PHP coding....
     
    jimmy4feb, Jun 11, 2010 IP
  6. ram4nd

    ram4nd Active Member

    Messages:
    167
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    50
    #6
    Did you try what I told you?
     
    ram4nd, Jun 12, 2010 IP
  7. Napoleon

    Napoleon Peon

    Messages:
    732
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Put ob_start() at the very top of the page inside php tags.
     
    Napoleon, Jun 12, 2010 IP
  8. Napoleon

    Napoleon Peon

    Messages:
    732
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #8
    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.
     
    Napoleon, Jun 12, 2010 IP