Here we are solving a basic problem faced by a beginner web developer in PHP i.e. Storing the data from a form into a text file.

HTML code

The following HTML code will generate a simple form asking for email-id and name from user.

<!DOCTYPE html>
<form action="transfer.php" method="post">
NAME:<input type="text" name="NAME"  />
E-MAIL:<input type="text" name="emailid"  />
<input type="submit" value="Submit" />
<input type="reset" value="Reset" />

Note the action of the form that is redirecting the form to transfer.php, Code for transfer.php is as follows:

PHP code

$data = $name . "+" . $email . ",";


file_put_contents($file , $data . PHP_EOL , FILE_APPEND);

echo "Thanks for filling up the form";
In above PHP code, you can see that we declared a variable called $file in which we stored the name of the file in which the form data is to be stored. We used the predefined function in PHP called as file_put_contents() to transfer the form data into the text file on submitting the form. Contents of File.txt is as follows:


Data entered in the form will be saved like this in the text file.