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>
 
     
     
     
    