if($_SERVER[‘REQUEST_METHOD’] == ‘POST’){
if($username && $age && $education && $comments = “”){
echo(“Fill out all the fields and return again.”);
} else {
fwrite( $file, "Your Username : " .$username. “\n” );
fwrite( $file, "\n Your Age : " .$age. “\n” );
fwrite( $file, "\n Your Education : " .$education. “\n” );
fwrite( $file, "\n Your Comments : " .$comments. “\n” );
fwrite( $file, “This documents is created by Macro Project By : Akhil Kokani\n” );
echo("Data written into file successfully. URL : " .$filename. “ ”);
fclose( $file );
}
}
?>
When I execute the script
It executes the file properly, but only the problem is that even if I add comments, it is not writing comments in the output2.txt file
WHY?
You should put all those $_POST values inside if($_SERVER['REQUEST_METHOD'] == 'POST') because you will most likely get undefined index on when you try to access that file directly.
Something else. You haven’t given us your form markup, but it looks like you are relying on placeholders to inform users what to enter. This is bad practice for 2 reasons. Firstly, it is not accessible - screenreaders will not read placeholders; secondly, the placeholders will not be displayed if someone is using auto-complete. Each form field needs to have a label tag.
just sets the $comments field to be empty anyway, because of the missing = sign, and the assignment always evaluates to ‘true’ because it’s always able to execute it.