Store HTML Form Data in Text File Using PHP - CodeSpeedy (2024)

Hello Programmers, in this post I am going to 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 a 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.

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

Below I have provided the PHPcode 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 from the post data that is provided below.
data.txt is a file that we have to create for storing our form submission data in it.

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

Now below is given the HTML part for creating the form:

<!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>

From the above form our data be submitted.

Now I think you have understood the thing.

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

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 useisset()

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

something like this one

if(isset($_POST['value'])) { //////your code }
Store HTML Form Data in Text File Using PHP - CodeSpeedy (2024)
Top Articles
Latest Posts
Article information

Author: Prof. Nancy Dach

Last Updated:

Views: 5881

Rating: 4.7 / 5 (57 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Prof. Nancy Dach

Birthday: 1993-08-23

Address: 569 Waelchi Ports, South Blainebury, LA 11589

Phone: +9958996486049

Job: Sales Manager

Hobby: Web surfing, Scuba diving, Mountaineering, Writing, Sailing, Dance, Blacksmithing

Introduction: My name is Prof. Nancy Dach, I am a lively, joyous, courageous, lovely, tender, charming, open person who loves writing and wants to share my knowledge and understanding with you.