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-

<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" />
$fname = $_POST["fname"];
$lname = $_POST["lname"];
$file = fopen("file.txt", "a") // a = appends to file
fwrite($file, $fname . $lname);

Read more here:

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: