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

Warning: Cannot modify header information - headers already sent by

I have made a form, exactly as in the tutorials and it does send an email, but I also get this warning, instead of the thanks message. I already tried ob_server(), but that didn't change anything...

Warning: Cannot modify header information - headers already sent by (output started at /home/owesten/domains/odinwesten.nl/public_html/test/contact.php:1) in /home/owesten/domains/odinwesten.nl/public_html/test/contact.php on line 43

This is the site: http://odinwesten.nl/test/contact.php

This is my code:

<?php 
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = trim($_POST["name"]);
    $email = trim($_POST["email"]);
    $message = trim($_POST["message"]);

    if ($name == "" OR $email == "" OR $message == "") {
        $error_message = "U moet een naam, e-mailadres en een bericht intypen";
    }

    if (!isset($error_message)) {
        foreach ( $_POST as $value) {
            if (stripos($value, 'Content-Type:') != FALSE ) {
                $error_message = "Er is een probleem met de informatie die u heeft ingevoerd.";
            }
        }
    }

    if (!isset($error_message) && $_POST["address"] != "") {
        $error_message = "Er gaat iets fout met het verzenden van het bericht.";    
    }

    require_once("inc/phpmailer/class.phpmailer.php");
    $mail = new PHPMailer();

    if (!isset($error_message) && !$mail->ValidateAddress($email)){
        $error_message = "U moet een geldig e-mailadres doorgeven.";
    }

    if (!isset($error_message)) {
        $email_body = "";
        $email_body = $email_body . "Naam: " . $name . "<br>";
        $email_body = $email_body . "E-mailadres: " . $email . "<br>";
        $email_body = $email_body . "Bericht: " . $message;

        $mail->SetFrom($email, $name);
        $address = "sljcranen@gmail.com";
        $mail->AddAddress($address, "Odin Westen");
        $mail->Subject    = "Odin Westen Fanmail | " . $name;
        $mail->MsgHTML($email_body); 

        if($mail->Send()) {
            header("Location: contact.php?status=thanks");
            exit;
        } else {
            $error_message = "Er is een probleem met het verzenden van het bericht: " . $mail->ErrorInfo;
        }
    }
}
?><?php 
$pageTitle = "Odin Westen | Contact";
$section = "contact";
include('inc/header.php'); ?>

    <section>
        <div id="content">
            <h2>Contactgegevens</h2>
            <p>Odin Westen<br />Gringelstraat 5B<br />6412 AK Heerlen</p>
            <p>Tel nr: <a href="tel:+31616082449">06 160 824 49</a></p>
            <p>Email: <a href="mailto: &#105;&#110;&#102;&#111;&#064;&#111;&#100;&#105;&#110;&#119;&#101;&#115;&#116;&#101;&#110;&#046;&#110;&#108;">                                                                       &#105;&#110;&#102;&#111;&#064;&#111;&#100;&#105;&#110;&#119;&#101;&#115;&#116;&#101;&#110;&#046;&#110;&#108;</a></p>

            <div id="socmed">
                <h2>Social Media</h2>
                <a href="https://www.facebook.com/odin.westen" target="_blank"><img src="img/Facebook.png" alt="Facebook" /></a>
                <a href="http://www.linkedin.com/profile/view?id=82899297&trk=tyah&trkInfo=tas%3Aodin%20we%2Cidx%3A1-1-1&locale=en_US" target="_blank"><img src="img/LinkedIn.png" alt="LinkedIn" /></a>
            </div>

            <h2>Webdesign</h2>
            <p>Website door <a href="http://www.suzannecranen.com">Suzanne Cranen</a></p>  

        </div>
    </section>

    <section id="formulier">
        <div>

            <?php if (isset($_GET["status"]) AND $_GET["status"] == "thanks") { ?>

                <p>Bedankt voor het bericht! U krijgt zo snel mogelijk antwoord.</p>

            <?php } else { ?>

                <h2>Stuur Odin een bericht</h2>

                <?php
                    if (!isset($error_message)) {
                        echo '<p>Vul alle velden in om het bericht te verzenden</p>';
                    } else {
                        echo '<p class="message">' . $error_message . '</p>';
                    }
                ?>            

            <form method="post" action="contact.php">
                <table>
                    <tr>
                        <th>
                        <label for="name">Naam</label>
                        </th>
                        <td>
                        <input type="text" name="name" id="name" value="<?php if(isset($name)){ echo htmlspecialchars($name); } ?>">
                        </td>
                    </tr>
                    <tr>
                        <th>
                        <label for="email">E-mail</label>
                        </th>
                        <td>
                        <input type="text" name="email" id="email" value="<?php if(isset($email)){ echo htmlspecialchars($email); } ?>">
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                        <textarea name="message" id="message"><?php if(isset($message)){ echo htmlspecialchars($message); } ?></textarea>
                        <td>
                    </tr>
                    <tr style="display: none;">
                        <td>                             
                        <label for="address">Address</label>
                        <textarea name="address" id="address"></textarea>
                        <p>Mensen: Laat dit veld leeg!.</p>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                        <input type="submit" value="Verzend">
                        </td>
                    </tr>
                </table>
             </form>

                <?php } ?>
        </div>
    </section>

<?php
include('inc/footer.php');
?>

3 Answers

I first made the form as in the tutorials of Randy Hoyt, and then I didn't wanted the label message in the form, so I deleted it and that is why it didn't work at my site. To find this problem, I downloaded the project files of the tutorial and changed the contact.php of the Shirts 4 Mike site to a contact site for my site (I just changed the errors to Dutch and added some contact information. The PHP and the form stayed the same.

Don Shipley
Don Shipley
19,488 Points

Can you post why it did not work? I have the same issue with my live server all though my header() works on my localhost

Don Shipley
Don Shipley
19,488 Points

I tried changing the header() to header("location: http:www.donshipley/thankyou.php") instead of Randy Hoyt status thanks and still after filling out the form and submit it goes to a white page... not what I want .. Oh well will get it sooner or later. Thank you for your reply