Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Enhancing a Simple PHP Application Integrating Validation Errors Displaying the Error Message

Luke Lee
Luke Lee
7,577 Points

Warning: Cannot modify header information - headers already sent by

I received an error message: Warning: Cannot modify header information - headers already sent by Below is my code, which is almost the same as the tutorial, can anyone please tell me what's wrong is my code? thanks:

    if ($_SERVER["REQUEST_METHOD"] == "POST") {

    $name = trim($_POST["name"]);
    $message = trim($_POST["message"]);
    $email = trim($_POST["email"]);
    $na_fill = trim($_POST["na_fill"]);

    ini_set('display_errors', 'On');
    error_reporting(E_ALL);

    if($name=="" || $message=="" || $email==""){
        $error_message = "Mandontory fields can't be empty.";
    }

    if(!isset($error_message)){
        foreach($_POST as $value){
            if( stripos($value,'Content-Type:') !== FALSE ){
                $error_message = "There was a problem with the information you entered.";
            }
        }
    }

    if($na_fill!="" && !isset($error_message)) {
        $error_message = "Your submission form has an error.";
    }

    require_once("class.phpmailer.php");

    $mail = new phpmailer();

    if(!isset($error_message) && !$mail -> ValidateAddress($email)){
        $error_message = "Your email is not valid.";
    }

    if(!isset($error_message)){
        $email_body = "";
        $email_body = $email_body. "Name: " . $name;
        $email_body = $email_body. "Email: ". $email;
        $email_body = $email_body. "Message: ". $message;

        $mail->SetFrom($email, $name);
        $address = "dlsm90@hotmail.com";
        $mail ->AddAddress($address, $name);
        $mail ->Subject = "Enquiry from phpmailer | ".$name;

        $mail ->MsgHTML($email_body);
        if($mail -> send()){
            header("Location: ?status=thanks");
            exit;
        }
        else {
            $error_message = "There was a problem sending the email: " . $mail->ErrorInfo;
        }
    }
} ?>

<?php
 if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>
        <p>Thanks for the email! I&rsquo;ll be in touch shortly!</p>
    <?php } else { ?>
    <?php
            if (!isset($error_message)) {
                echo '<p>I&rsquo;d love to hear from you! Complete the form to send me an email.</p>';
            } else {
                echo '<p class="message">' . $error_message . '</p>';
            }
        ?>
<form method="post">
    <input name="name" type="text"><br />
    <input name="email" type="email"><br />
    <textarea name="message" id="message"></textarea><br />
    <input name="na_fill" type="text" style="display:none;"><br />
    <input type="submit" value="Send">
</form>
<?php } ?>

4 Answers

Luke Lee
Luke Lee
7,577 Points

Alright, it is fixed: Just removed the <html><head><body> wrap. If anyone has this problem, check this web page:

http://www.arclab.com/en/webformbuilder/php-warning-cannot-modify-header-information-headers-already-sent.html

Luke Lee
Luke Lee
7,577 Points

why does the Markdown only work part of my codes??

Scott Evans
Scott Evans
4,236 Points

If anything is being output out by your script before the header(); tag, this will give you an error. Make sure nothing is being output.

Could you provide a link to this site, so i could get a better understanding?

Luke Lee
Luke Lee
7,577 Points

Hi, Scott: Thank you for help. I don't have the link, this is the tutorial excise.

Luke Lee
Luke Lee
7,577 Points

Hi, Kennard: I don't see any header() in class.phpmailer.php. I followed the tutorial and apply the code onto another form. somehow this issue occurs. I searched on Google, seemed this was a very common issue, but I just couldn't find any answer.