Reputation: 11696
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>
Upvotes: 0
Views: 190
Reputation: 423
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.
Upvotes: 1
Reputation: 360762
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...
Upvotes: 2
Reputation: 605
all html, even title will throw your error. Take the tags out and all HTML, and try again.
Upvotes: 1