Skip to Content

How to save HTML form data to a text file using PHP

How to save HTML form data to a text file using PHP

Today, we will discuss how to save HTML form data to a text file using PHP.

While using HTML, we often use forms to capture some data from users. We usually store this captured data into the database, but sometimes we need to keep this captured data into a text file.

Captured data may be confidential. So, storing sensitive data in a machine is not recommended at all unless there are some robust security mechanisms.

Let’s try to learn how we could achieve this, but as always, there are some prerequisites.

  • It would be best if you had a strong understanding of HTML and PHP.
  • You should have a functional server. You could also use local servers like XAMP.

Below is the stepwise process of saving the form data into a text file.

 

1.Creating an HTML form

If you already have created an HTML form, this step is not needed; otherwise, you can create an HTML form to capture some user data. Always use the POST method to send data to the server. Below is an example form.

 

<form method="post">
      <input type="text" name="firstName" placeholder="First Name" required autocomplete="off"> <br>
      <input type="text" name="lastName" placeholder="Last Name" required autocomplete="off"> <br>
      <input type="submit" name="submit" value="Submit">
    </form>

 

We have created a form using the POST method. For the sake of simplicity, we have just completed creating two text fields and a submit button.

 

2.Writing the server-side PHP script

Now we would be writing the server-side script to handle this form data. Usually, we receive this form data and store it in a database, but in this case, we would be keeping it into a text file instead of saving it into the database.

We may do both of these tasks within a single script, but we will only be saving data into the form for the sake of simplicity. Below is an example script.

 

<?php
if(isset($_POST['submit'])){
$firstName = "First Name:".$_POST['firstName']."
";
$lastName = "Last Name:".$_POST['lastName']."
";
$file=fopen("file.txt", "a");
fwrite($file, $firstName);
fwrite($file, $lastName);
fclose($file);
}
?>

 

In first statement if(isset($_POST[‘submit’])), we are checking if the submit button has been clicked. If it has been clicked, then we are performing some tasks within the if statement.

We are assigning value captured by input text element “firstName” to the PHP variable “firstName”. We are doing the same for the input text element “lastName”.

Next, we are creating a PHP variable file and assigning a text file “file.txt” to it in “write-only” mode. “a” indicates the write-only mode. This “fopen” function will create the file, and if the file exists already, it will append to it.

Then we are writing variables $firstName, $lastName to the file with fwrite() function and closing the file in the end. The file may be formatted using spaces or newlines between the data items, but we have not done that to keep it simple.

I hope we have been able to shed light on how to save HTML form data to a text file using PHP.