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

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

 }

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

  1. Fanon says:

    Great !!! 🙂

  2. Roy says:

    Thanks alot!
    Just two problems; 1. it prints the number 1, no idea why 2. I want each submit in new line inside the txt file

    All the best!

  3. A Bandyopadhyay says:

    If I put the data in Bengali font, then the created txt file stores something like &#1745, &#2348 etc. How to solve this problem? Please help.

  4. mamad nobari says:

    thank you so much. its so helpful

  5. BB says:

    Hi, I keep on getting this error: ERROR:crash_report_database_win.cc(428)] unexpected header
    Could you please advise.
    Thank you for the post!

    • Saruque Ahamed Mollick says:

      Most probably you are getting this error because you are using old chrome which is broken. Try to uninstall chrome and reinstall it. It will fix your problem.

  6. ashu says:

    what are system requirement as I am running it on my laptop and data.txt file not saving any data

    • Saruque Ahamed Mollick says:

      Kindly save the file as php and run on your server. If you are using localhost then start the server and run.

  7. Joez says:

    Thanks :DDDD

  8. PRATHMESH says:

    how to save form data/input when i click on SUBMIT button & clear from data in html.

  9. imran says:

    Do I need to use localhost/xampp for this?

  10. Andrés Ospina says:

    Hi, thanks for the explanation. Why does it work perfectly in the localhost, like xampp (I mean, the data is stored in the text file), but in the remote server the data is not stored in the text file? Thank you again

  11. Anonymous says:

    How can you have more than one line. And the HTML code doesn’t do anything.

  12. Nasir Noor Muhammad says:

    I have set all thing but data.txt is still blank

  13. Andrin says:

    Hi. Thank you, this looks amazing! How can I save every form entry in a separate text file?

  14. Jordan says:

    Thank you so much. I wish that my school’s text book were this clear. My book had be tearing my hair out, but this was sooo simple and easy to understand. Thank you thank you!!!!

  15. Mehvish says:

    I used exact same code you provided and its working fine.
    But when i want to use it in my form where there are 5 textboxes, its not working. Can you please show me how to use it with 5 textboxes, First name, last name, phone number, email and comments- and a submit button.

  16. Aero says:

    Thanks it worked. Can anyone suggest a few improvements to this code? for example:
    1. Success Message upon submitting form
    2. Info saved in data.txt, have each entry saved on a new line or separated by a spacial character.

  17. Seybanks says:

    Remember to sanitize post data e.g with htmlspecialchars($_POST[‘data’] );

Leave a Reply

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