I'm struggling with my code because it's a little bit complex than having a form save its data to a text file. I wanted the form to show conditional messages like if an empty form is submitted a "please enter comment" will show, if finished is entered it will show that the data is accepted and a "congratulations" will appear. If it is any other data, it will simply say that data is received and "thank you for the comment" will appear. I was able to do this but for some reason I can't make the entered data from the php file save to the text file. Here is my code:
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset ="UTF-8">
<title>mission_2-02</title>
</head>
<body>
<form action="" method="post">
<input type="text" name="comment" placeholder="comment">
<input type="submit" name="submit">
</form>
<?php
$str = $comment . PHP_EOL;
$filename="mission_2-02.txt";
$fp = fopen($filename,"w");
$comment = $_POST ["comment"];
if ($comment==""){echo "";}
else echo $comment . "is accepted <br>";
if (empty ($comment)) {echo"please enter comment";}
elseif ($comment=="finished"){echo "congratulations";}
elseif (!empty($comment)){echo "thank you for the comment";}
else{fwrite($fp, $str );echo $str ;fclose($fp);}
if (file_exists($filename)){
$lines = file($filename,FILE_IGNORE_NEW_LINES);
foreach ($lines as $line){
echo $line . "<br>";
}
}
?>
</body>
</html>
</head>must come before the opening<body>tag and there should be a space betweenmetaandcharsetin<metacharset ="UTF-8">$str = $comment . PHP_EOL;~ at that stage it has not been created