Monday 8 July 2019

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.
  1. <?php
  2. if(isset($_POST['textdata']))
  3. {
  4. $data=$_POST['textdata'];
  5. $fp = fopen('data.txt', 'a');
  6. fwrite($fp, $data);
  7. fclose($fp);
  8. }
  9. ?>
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`
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Store form data in .txt file</title>
  5. </head>
  6. <body>
  7. <form method="post">
  8. Enter Your Text Here:<br>
  9. <input type="text" name="textdata"><br>
  10. <input type="submit" name="submit">
  11. </form>
  12. </body>
  13. </html>
Now I think you have understood the thing.

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.
    1. <!DOCTYPE html>
    2. <html>
    3. <head>
    4. <title>Store form data in .txt file</title>
    5. </head>
    6. <body>
    7. <form method="post">
    8. Enter Your Text Here:<br>
    9. <input type="text" name="textdata"><br>
    10. <input type="submit" name="submit">
    11. </form>
    12. </body>
    13. </html>
    14. <?php
    15. if(isset($_POST['textdata']))
    16. {
    17. $data=$_POST['textdata'];
    18. $fp = fopen('data.txt', 'a');
    19. fwrite($fp, $data);
    20. fclose($fp);
    21. }
    22. ?>

  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.

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

 }

0 comments:

Post a Comment