Question

I have a heredoc form (shown below) inside addToDb.php. The form posts back to addToDb.php when the user clicks the submit button on the form (the 'ADD RECORD' button in the form below).

After verifying that I have okay data on the form, I add the data to my database and use header() to 303 redirect (this is the my 1st attempt ever to use post/redirect/get).

I get an error:

"Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\foo\addToDb.php:165) in C:\xampp\htdocs\foo\addToDb.php on line 129 and in C:\xampp\htdocs\foo\addToDb.php on line 130"

 echo <<<_END
  <h1>Please add a new student....</h1>
  <p>Fields with an asterisk (*) are required fields.</p>
  <form action="addToDb.php" method="post">
    <b><i>First name *</b></i> : <input type="text" name=$firstNameLabel /><br />
 Middle initial  : <input type="text" name=$middleInitialLabel /><br />
     <b><i>Last name *</b></i> : <input type="text" name=$lastNameLabel /><br />
       <b><i>School*</i></b> : <input type="text" name=$schoolLabel size=40/><br />
      Grade: <input type="text" name=$gradeLabel /><br />
         <b><i>Email *</b></i> : <input type="text" name=$emailLabel size=40 /><br />
    <input type="submit" name="addrecord" value="ADD RECORD" /><br />
</form>
_END;

*(NOTE: the _END above, which closes the heredoc, appears on line 165 in addToDb.php -- that line 165 is mentioned in the error message I get.)*

So when the 'ADD RECORD' submit button is pushed, the same addToDb.php file is re-entered and the same form as above is displayed again.

When addToDb.php is re-entered due to the submit, I check for valid form data supplied by the user and save to the database if it's valid data and call header() as follows:

// check for valid form data and add to the database, then redirect now.........
header("HTTP/1.1 303 See Other");
header("Location: http://localhost/foo/index.php");
exit();

(NOTE: the two calls to header above appear on lines 129 and 130 in addToDb.php)

So here's what happens:

  • I fill in the form with valid data
  • push 'ADD RECORD'
  • then the form is re-displayed as I re-enter addToDb.php
  • my record successfully gets added to the database, but......
  • I do not see index.php in the browser, I still see addToDb.php
  • and I get the error message as described above and repeated below:

"Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\foo\addToDb.php:165) in C:\xampp\htdocs\foo\addToDb.php on line 129 and in C:\xampp\htdocs\foo\addToDb.php on line 130"

I have used the heredoc approach in the past with no problems.

But this is the very first time in my life I am trying to implement Post/Redirect/Get.

So -- is the heredoc somehow interfering with my PRG?

Was it helpful?

Solution

The headers must be the first thing sent to the client. You can do anything you like in the code first (i.e. test the post/get variables) as long as you don't send any data to the client (i.e. use echo or place plain html outside of php tags) before sending the http headers. After the headers are sent you can, of course, do what ever you like.

So you are getting the error:

Cannot modify header information - headers already sent by...

The headers were automatically provided at the beginning of the document, because you didn't provide yours there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top