Save Data To Text File Using PHP
Submitted by argie on Tuesday, March 4, 2014 - 14:00.
This tutorial will teach you on how to save data on text file using php. The feature of this tutorial is it will allow you to save information without using any database. It also provide a confirmation message that you have successfully save the data in the text file. To understand more this tutorial follow the steps bellow.
That's it you've been successfully a system that save data to text file.
Creating Our Form
The code bellow will display the form and the confirmation message generate by our php server. Copy the code bellow and save it as "index.php".- <?php
- foreach($_SESSION['ERRMSG_ARR'] as $msg) {
- echo '<span style="color:red;">',$msg,'</span>';
- }
- }
- ?>
- <form action="save.php" method="POST">
- <span style="display: inline-block; width: 150px;">Name</span><input type="text" name="name" required="required" /><br>
- <span style="display: inline-block; width: 150px;">Age</span><input type="text" name="age" required="required" /><br>
- <span style="display: inline-block; width: 150px;">Gender</span><select name="gender"><option>male</option><option>female</option></select><br>
- <span style="display: inline-block; width: 150px;"> </span><input type="submit" value="Save" />
- </form>
Creating Our Save Scripts
the code bellow will allows us to save data into the text file using the "fwrite" function in php. Copy the code bellow and save it as "save.php".- <?php
- $errflag = false;
- $filename = "informationlist.txt";
- $newline=$name.','.$age.','.$gender."\r\n";
- $errmsg_arr[] = $name.' data has been save to '.$filename.' file';
- $errflag = true;
- if($errflag) {
- $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
- }
- die;
- ?>
Comments
Add new comment
- Add new comment
- 1491 views