You can save your web form data in a text file instead of saving it in a MySQL table using a simple PHP script. The advantage of doing this is that you might not have the MySQL server installed on your server or you might like to save your web form data in another form.
In fact if you save your web form data in a text file as comma separated values (CSV) you can later on import your data in any file format. For instance, after downloading your text file, you can import the values in an Excel sheet, or into an Access table, or later on, even in a MySQL table. You can use it with any software the uses comma separated values.
Saving web form data in a text file with PHP
Why PHP? First of all, I’m more comfortable with PHP. Second, in order to achieve this you need some server side scripting and there is a 99.99% chance that your web host allows PHP scripts. So that’s now out of our way.
Suppose you have the following web form on your website or blog:
<form name=”web_form” id=”web_form” method=”post” action=”process-form-data.php”>
<p><label>Enter name: </label><input type=”text” name=”name” id=”name” /></p>
<p><label>Enter email: </label><input type=”text” name=”email” id=”email” /></p>
<p><input type=”submit” name=”s1″ id=”s1″ value=”Submit” /></p>
Since we only need an example so we’ll have just two fields for the form, namely name and email. Next, create a text file containing this line:
and name it as, say, formdata.txt. As you can see, it has a comma between the two headers. Also press the Enter key once after writing Email just so that the end of file pointer is in the next line. Upload this file to the folder where you have your web form file and the PHP script file that will save your form data in this text file. Make sure your text file is writable on the server (the file permission should be set to 666) and you can do it by right-clicking it through your FTP client.
Now to the file where the real action happens, that is, “process-form-data.php”. This file contains the following lines of code:
$name = $_POST['name'];
$email = $_POST['email'];
$fp = fopen(”formdata.txt”, “a”);
$savestring = $name . “,” . $email . “n”;
echo “<h1>You data has been saved in a text file!</h1>”;
The first two lines saves the web form post data in local PHP variables. The 3rd line opens the text with the appropriate permission. In PHP we can open files with difference permissions, but here, “a” means open the file in write-mode and append the new information (without overwriting the existing information. You can refer to PHP official documentation for all file opening permissions. Just remember that a wrong permission can delete your file or erase all your data.
The 4th line creates a string that combines the two values — name and email — puts a comma between them and then inserts the new line character so that the end of file pointer moves to the next line.
The fwrite function writes the newly-created line and the remaining code is very easy to understand.
If you are not saving very critical data, saving your web form data in a text file is better than saving it in a database table as then it is easier to access it without extra programming knowledge.