Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Build a Basic PHP Website (2018) Adding a Basic Form Redirecting After a Form Submission

Rob Nanya
Rob Nanya
8,379 Points

Warning: Cannot modify header information - headers already sent by

I'm following along with the video instructions but get an error message instead of having the page go to the thanks.php page

Any idea what's causing it?

If I take out the header() function it works.

<?php 
///var_dump($_POST);

$name = $_POST["name"];
$email = $_POST["email"];
$details = $_POST["details"];

echo "<pre>"; // this is line 8
$email_body = "Name " . $name . "\n"
    . "Email " . $email . "\n"
    . "Details " . $details . "\n";
echo $email_body;
echo  "</pre>";

// To Do: Send Email
header("location:thanks.php"); // this is line 16
?>

Warning: Cannot modify header information - headers already sent by (output started at Test/Basic PHP Website/process.php:8) in /Test/Basic PHP Website/process.php on line 16

6 Answers

Rob Nanya
Rob Nanya
8,379 Points

My data.php file had the following

<!--Catalog items-->

<?php
$catalog....

Make sure not to have any extra spaces or lines. I removed

<!--Catalog items-->

and it worked.

Rob Nanya
Rob Nanya
8,379 Points

The only thing I can think off from reading this page: http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php

It mentions this error might be because I have the ECHO statement before the header() function. But the instructor in the video does just that!?!

Rob Nanya
Rob Nanya
8,379 Points

Also the PHP manual states "must be called before any actual output is sent" http://php.net/manual/en/function.header.php

Rob Nanya
Rob Nanya
8,379 Points

Can't proceed with my track. Looks like the following video tutorial tie into this file and it breaks the page. Any advice on how to troubleshoot would be appreciated. Please let me know if I should submit other related files that are being referenced if you need more info.

Baptiste BOULONNAIS
Baptiste BOULONNAIS
9,845 Points

Hi Rob, I've had the same problem with MAMP locally. I turned outpout buffering to "On" on my php.ini file. This post was usefull :

https://teamtreehouse.com/community/form-redirect-to-thanksphp

Rob Nanya
Rob Nanya
8,379 Points

I have the same problem when using the header function on a different page / video assignment. Think these two errors are related: https://teamtreehouse.com/community/code-block-breaks-my-page