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 Build a Simple PHP Application Adding a Contact Form Working with Get Variables

Can't figure out the bug!

I have the same code, but where I try to press submit the site is redirecting me in blank page where is written: Warning: Cannot modify header information - headers already sent by (output started at /Applications/MAMP/htdocs/shirts4mike/contact.php:2) in /Applications/MAMP/htdocs/shirts4mike/contact.php on line 14 Can someone help me?

Can you post here the contact.php page please?

<?php #contact-process.php
if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $name = $_POST['name'];
  $email = $_POST['email'];
  $message = $_POST['message'];
  $email_body = "";
  $email_body = $email_body . "Name: " . $name ."\n";
  $email_body = $email_body . "Email: " . $email ."\n";
  $email_body = $email_body . "Message: " . $message ."\n";

  // todo: Send EMail

  header("Location: contact.php?status=thanks");
  exit;
}
#End contact-process.php
?>

<?php
  $page_title = "Contact Mike";
  $section = "contact";
  include('includes/header.php');
?>

  <div class="section page">
    <div class="wrapper">
      <h1>Contact</h1>

      <?php if (isset($_GET["status"]) && $_GET["status"] == "thanks") { ?>
        <p>Thanks for the email! I'll be in touch shortly!</p>
      <?php } else { ?>

        <p>I'd love to hear from you! Complete the form to send me and email.</p>

        <form method="post" action="contact.php">
          <table>
            <tr>
              <th>
                <label for="name">Name</label>
              </th>
              <td>
                <input type="text" name="name" id="name">
              </td>
            </tr>

            <tr>
              <th>
                <label for="email">Email</label>
              </th>
              <td>
                <input type="text" name="email" id="email">
              </td>
            </tr>

            <tr>
              <th>
                <label for="message">Message</label>
              </th>
              <td>
                <textarea name="message" id="message"></textarea>
              </td>
            </tr>
          </table>
          <input type="submit" value="Send">

        </form>

      <?php } ?>

    </div>
  </div>

<!-- Including Footer -->
<?php include('includes/footer.php'); ?>

3 Answers

Jeff Lemay
Jeff Lemay
14,268 Points

PHP's header function will not work if anything is being printed to the page before you call the function. It's okay to have if/else statements and other php before you call the header function to go to a new page, but if you have an empty line or space at the top of your document before opening your php tags, the header will fail. Frustrating.

I can't see specifically what's causing your issue in the code you posted. Here's in-depth details to help you troubleshoot:

http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php

Try moving the comment at the top of the page to the next line. Make sure there are no spaces after the open tag: <?php

Thanks, I even can't imagine that this was a problem.