How to write for data to .txt file?

How can i use html and php to simply make a form that when filled out by the user writes the data to a text file in the same directory (data.txt)? So far anything I've tried just didn't work. I've tried-

<!--HTML-->
<form method="POST" action="save.php">
  <input type="text" id="fname" name="fname" placeholder="First Name" />
  <input type="text" id="lname" name="lname" placeholder="Last Name" />
  <input type="submit" />
</form>
//PHP
$fname = $_POST["fname"];
$lname = $_POST["lname"];
$file = fopen("file.txt", "a") // a = appends to file
fwrite($file, $fname . $lname);
fclose($file);
exit(0);


Read more here: https://stackoverflow.com/questions/64408783/how-to-write-for-data-to-txt-file

Content Attribution

This content was originally published by im-new-to-coding-dont-hate at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: