Save HTML Form Data in a (.txt) Text File in PHP
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 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 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.
$data is 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
- 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); } ?>
- create a new file in the same directory or folder & name it data.txt and save it.
- 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 }
Great !!! 🙂
Thank you very much 🙂
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!
If I put the data in Bengali font, then the created txt file stores something like ۑ, ब etc. How to solve this problem? Please help.
UTF-8 without BOM signature. Use the text editor(s) that supports encoding.
thank you so much. its so helpful
We are glad that this post helped you
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!
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.
what are system requirement as I am running it on my laptop and data.txt file not saving any data
Kindly save the file as php and run on your server. If you are using localhost then start the server and run.
Thanks :DDDD
how to save form data/input when i click on SUBMIT button & clear from data in html.
Do I need to use localhost/xampp for this?
As you are working in PHP then you must need to use a server. You can use xampp for this if you wish.
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
How can you have more than one line. And the HTML code doesn’t do anything.
I have set all thing but data.txt is still blank
Hi. Thank you, this looks amazing! How can I save every form entry in a separate text file?
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!!!!
Thank you very much. I am happy that this tutorial helped you to save form data in a txt file.
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.
Hi! I am not very good at php. Could you please tell me how to save the strings in different lines? For example:
FirstText
SecondText
Instead of:
FirstTextSecondText
How do I make a break line after every input in the text file?
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.
Remember to sanitize post data e.g with htmlspecialchars($_POST[‘data’] );
Can i write into file in html as input
How do i get it to make a new line after data is submitted
You should be able to append the file by adding the ‘a’ to the $fp definition:
$fp = fopen(‘data.txt’, ‘a’);
Awsome! It’s exactly what i’m finding all of my day!
Respect..
Does this work with apache? I attempted it on ubuntu. do you think the problem is my os?
Everything else worked but the text file stays blank.
Hello sir, I am trying to send email to the user who has entered his email in input field, I want to store his email in a variable and then send him email, my PHP file currently look like this:
public function saveData(Request $request){
$data = $request->all();
//getting the input field
$emails = $data[‘yemail’];
//storing Forms in a new var
$std = new Forms;
//assigning database columns to the input fields
$std->email = $emails;
//saving the data
$std->save();
$emails = $data[‘yemail’];
$user[‘to’]= $emails;
Mail::send(‘mail’,$data,function($messages) use ($user){
$messages->to($emails);
$messages->subject(‘Newsletter’);
return view(‘mail’);
}); return “Welcome to the Family!”;
}
this code is giving me error saying, $emails is undefined.
hello i’d try to run your sample code but i could not connect to the data file upon submission. please help me
cannot save my input data to the data file
hi i tried to do this and for some reason chrome reads .PHP files as plain test files instead of loading HTML is there a way around this?
sorry i meant text instead of test (btw am using chrome os)
txt data not shown line by line. the data file like this- comestoremanymonth