Save HTML Form Data in a (.txt) Text File in PHP

By Saruque Ahamed Mollick

Hello Programmers, in this post I gonna show you a very essential task that can be easily done with Core PHP and HTML form.
Sometimes it happens that we need to store some data in local storage file rather than making it complex using the database. Yes, it’s a fact that in many cases we don’t want to store our text data in database always.
Here I am giving you an example, suppose you have an HTML form and you want to store the data submitted by the user in a text file so that you can easily access it later from that file without opening your database.

PHP Program to store HTML Form data in a .txt File

Below I have provided the PHP code to store the form data in a text file. Just took a glance at this code.
For easy understanding after the code, I have provided the explanation and how to use this code step by step.

<?php
              
if(isset($_POST['textdata']))
{
$data=$_POST['textdata'];

$fp = fopen('data.txt', 'a');
fwrite($fp, $data);
fclose($fp);
}
?>

Here ‘textdata’ is the name of our HTML form field that is provided below.
data.txt is a file that we have to create for storing our form submission data in it.

$data is a PHP variable to store the form field data entered by the user.

Now the HTML part`

<!DOCTYPE html>
<html>
<head>
  <title>Store form data in .txt file</title>
</head>
<body>
  <form method="post">
    Enter Your Text Here:<br>
    <input type="text" name="textdata"><br>
    <input type="submit" name="submit">
    
  </form>

</body>
</html>

Now I think you have understood the thing.

If you are curious enough to know how to fetch or retrieve the text  data in PHP click the below link

Fetching Text Data From a Text File Using PHP



 

remember to add method in your form. <form method=”post”>

Step by step guide on How to put the HTML form field data in a text file or dot txt file in PHP

  1. Create a PHP file and put the below code and save it.
    <!DOCTYPE html>
    <html>
    <head>
      <title>Store form data in .txt file</title>
    </head>
    <body>
      <form method="post">
        Enter Your Text Here:<br>
        <input type="text" name="textdata"><br>
        <input type="submit" name="submit">
        
      </form>
    
    </body>
    </html>
    
    
    <?php
                  
    if(isset($_POST['textdata']))
    {
    $data=$_POST['textdata'];
    
    $fp = fopen('data.txt', 'a');
    
    fwrite($fp, $data);
    fclose($fp);
    }
    ?>

     

  2. create a new file in the same directory or folder & name it data.txt and save it.
  3. Now run the PHP file.
    enter any text and hit on submit button and check your data.txt file. Your text entered in the form is saved in your text file.

How to Count The Sub String from a .txt file using PHP

 

look at the below code. It will also work fine

<?php
            	
$data = $_POST['textdata'];
$fp = fopen('data.txt', 'a');
fwrite($fp, $data);
fclose($fp);

this will work fine. But in some servers it might show an error like this

“Undefined index: ”

in order to prevent the error warning we use isset()

so it’s safe to use this before $_POST[‘value’];

something like this one

if(isset($_POST['value'])) { 

       //////your code

 }

2 responses to “Save HTML Form Data in a (.txt) Text File in PHP”

  1. Fanon says:

    Great !!! 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *