Web Development Tutorials




  
  

Collecting Form Data

In most cases, form data enter by users is written to a RDMS such as MySQL using the MySQL functions discussed in previous sections. In a similar fashion, form data can also be written to a text file. This section describes how to use PHP to collect form data and write it to a text file.

First Name:       
 
Last Name:  
 
Current Time: 

Consider the form page shown above. The following example demonstrates how to write the submitted form data to a text file:

<?php

if (isset($_REQUEST['SubmitB']))

{")%>

$file_name = "c:\formfile.txt";
$open_file = fopen($file_name, "a+");

$file_contents= $_POST['FName'] . "," . $_POST['LName'] . "," . $_POST['DateTime'] ."\n";

fwrite($open_file,$file_contents);

fclose($open_file);

echo "Form data successfully written to file";

}

?>


<!DOCTYPE html>

<html>
<head>
<title>A Web Page</title>
</head>
<body>

<p>Writing Form Data To A File</p>

<p>
<form method="post" action="createfile.php">

Enter First Name <input type="text" name="FName"><br><br>
Enter Last Name  <input type="text" name="LName"><br><br>
<input type="hidden" name="DateTime" value="<?php echo date('g:i a') ?>">
<input type="submit" name="SubmitB" value="Submit Data">

</form>
</p>

</body>
</html>

In this example, the HTML form page contains a textbox for the user's first name and last name. A hidden field is also coded and includes the current date and time using the PHP date() function. When the form's submit button is clicked, a new text file called 'formfile.txt' is created and opened in append mode:


$file_name = "c:\formfile.txt";
$open_file = fopen($file_name, "a+");

Next, the variable, $file_contents, is assigned the values of the of the POST superglobal variables, containing the user's first name, last name, and the current date/time value. Commas are concatenated to the strings to create delimiters separating each value. The newline character is added to create a carriage return at the end of each line:

$file_contents= $_POST['FName'] . "," . $_POST['LName'] . "," . $_POST['DateTime'] ."\n"; 

Finally, the contents of the variable, $file_contents, are written (appended) to the text file. The file is closed and a the echo statement is used to display a confirmation message to the browser window:

fclose($open_file);
echo "Form data successfully written to file";

In this example, the HTML form page contains a textbox for the user's first name and last name. A hidden field is also coded and includes the current date and time using the PHP date() function. When the form's submit button is clicked, a new text file called 'formfile.txt' is created and opened in append mode:

$file_name = "c:\formfile.txt"; 
$open_file = fopen($file_name, "a+"); 

Next, the variable, $file_contents, is assigned the values of the of the POST superglobal variables, containing the user's first name, last name, and the current date/time value. Commas are concatenated to the strings to create delimiters separating each value. The newline character is added to create a carriage return at the end of each line:

$file_contents= $_POST['FName'] . "," . $_POST['LName'] . "," . $_POST['DateTime'] ."\n"; 

Finally, the contents of the variable, $file_contents, are written (appended) to the text file. The file is closed and a the echo statement is used to display a confirmation message to the browser window:

fclose($open_file); 
echo "Form data successfully written to file";

TOP | NEXT: Uploading Files