Storing Data
When you save data collected from an HTML form, you can do it in one of two ways, as a text file, or as data in a database. For these lessons you will save your collected data in a text file. To write data to a file takes three steps; open the file(if it doesn't exist, create it); write the data to the file; close the file.
Server Variables
To open or create the file we are going to use a server variable . A server variable provides information about the server hosting your website.
The server variable we are using is called DOCUMENT_ROOT, and returns the location of the root directory of your website. We are going to create a variable to store this information as follows;
$DOCUMENT_ROOT = $_SERVER[‘DOCUMENT_ROOT'];
This will be where we store our text file.
Opening the file
To open the file we use the fopen() function. This function needs three pieces of information; the name and location of the file you want to read/write to, and the mode or format you want to use (read, write or append). We will create a variable called $text_file to store the results of this function using the append (ab) mode;
$text_file = fopen( "$DOCUMENT_ROOT/orders.txt", 'ab')
Preparing the Order
To prepare the order we create one variable which has all the information in it. We use the string operator (.) to join together all the parts of the form. I have also added the time and date to the order by creating an extra variable to store the info. In the code below, the sections which are inside the double quotes will be printed as is, except the \t will create a tab. and \n create a newline (carriage return).
$date = date('H:i, jS F');
$order = $date."\t".$hd_qty." hard drives \t".$mon_qty.
" monitors\t".$pr_qty." printers\t\$".$total_amount."\n";
Writing the Data
Next we write the data to the file using the fwrite() function. This function needs three pieces of information, the location of the text file to write to (stored in $text_file), the data to be written (stored in $order), and the amount of data to be written. To work out the third piece of information we use the strlen() function which counts up how many characters there are in the variable $order, and passes that number to the fwrite() function.
fwrite($orderform, $order, strlen($order));
Closing the File
Its important to close the file when you've finished.
fclose($orderform);
Adding Code to the ProcessOrder Page
Add this code to the bottom of the php cody in the body of your HTML page. I have added a further refinement, a lock function to stop two people trying to write to the file at the same time. it has two values, LOCK_EX (locks the file for your exclusive use) and LOCK_UN (unlocks the file).
// Create a formatted string to put in the text file
$date = date('H:i, jS F');
$order = $date."\t".$hd_qty." hard drives \t".$mon_qty." monitors\t"
.$pr_qty." printers\t\$".$total_amount."\n";
// open file for appending
$orderform = fopen("$DOCUMENT_ROOT/orders.txt", 'ab');
flock($orderform, LOCK_EX);
fwrite($orderform, $order, strlen($order));
flock($orderform, LOCK_UN);
fclose($orderform);echo '<p>Order written.</p>';
To see the source code for the entire page, click here.
Next...Create a page to view the orders
