0

I'm not sure why this code is throwing a headers already sent. I've looked at: How to fix "Headers already sent" error in PHP

and am still getting the error. It's encoded in ANSI, I've typed it in notepad++, there seems to be no whitespace... any help would be appreciated it. The error is coming from line 8.

Code follows:

<!DOCTYPE html>
<html>
<head>
<title>Form Testing</title>
<meta charset="utf-8"/>
</head>
<body>
<?php 
  if($_POST['formSubmit'] == "Submit")  {
     $errorMessage = "";
     $varName = htmlspecialchars($_POST['formName'], ENT_QUOTES, 'UTF-8');
     $varMovie = htmlspecialchars($_POST['formMovie'], ENT_QUOTES, 'UTF-8');
     if(empty($varName)){
      $errorMessage .= "<li>You forgot to enter a name!</li>";
     }
     if(empty($varMovie)){
     $errorMessage .= "<li> You forgot to enter a movie!</li>";
     }
     if(!empty($errorMessage)){
        echo("<p> There was an error with your form: </p> \n");
        echo("<ul>".$errorMessage. "</ul> \n");
     }else{
        $fs = fopen("mydata.csv","a");
         fwrite($fs,$varName . ", " . $varMovie . "\n");
         fclose($fs);
         header("Location: ThankYou.html");
         echo "Hello ".$varName.". Your favorite movie is: ".$varMovie."!";
         exit;
    }
  }else{
    echo "Welcome! Please enter your name and movie preference";
  }
?>
<form action="index.php" method="post">
    Which is your favorite movie?
     <input type="text" name="formMovie" maxlength="50" value="<?=$varMovie;?>">
    What is your name?
    <input type="text" name="formName" maxlength="50" value="<?php echo $varName;?>">
    <input type="submit" name="formSubmit" value="Submit">
</form>

</body>
</html>
Community
  • 1
  • 1
user1357015
  • 11,168
  • 22
  • 66
  • 111
  • possible duplicate of [How to fix "Headers already sent" error in PHP](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) – Marc B Feb 15 '14 at 21:18

3 Answers3

2

Exactly what do you think the following is?

<!DOCTYPE html>              <---output
<html>                       <---output
<head>                       <---output
<title>Form Testing</title>  <---output
<meta charset="utf-8"/>      <---output
</head>                      <---output
<body>                       <---output
<?php 
...
header(...); // oops... output already performed...
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • Ah, I got it. I'm new to php and didn't realize that I could put the tags before the main tag. This makes sense. Thanks. – user1357015 Feb 15 '14 at 21:21
  • output is **ANYTHING** that comes out of that script, whether it's echo/print from PHP or just some raw text. Doesn't matter if you put a `` at the top of the script. The html STILL counts as output. – Marc B Feb 15 '14 at 21:21
  • Right, so I got it working by moving everything in to the very top. I then have the etc.. Thank you again for your help. – user1357015 Feb 15 '14 at 21:23
1

all html, even title will throw your error. Take the tags out and all HTML, and try again.

wribit
  • 605
  • 1
  • 6
  • 18
1

No output sending before header means really it!

    <!DOCTYPE html>
    <html>
    <head>
    <title>Form Testing</title>
    <meta charset="utf-8"/>
    </head>
    <body>

is output sent before header.

dirluca
  • 423
  • 3
  • 12