I've been writing PHP several months but I have not used flat files.
I cannot find a coding example in a book of saving form data to a flat file.
The process I want to create is:
Someone with a cookie opens a form. (The cookie is used to give a unique file name to a flat file that will hold form data.)
Code tests to see if cookie holder has been in form before.
If yes, opens file and assigns data to form's $a and $b.
If no, creates new file and formats file for $a and $b.
Cookie holder enters new data into $a and $b.
As cookie holder exits to new form, data in $a and $b is saved to flat file.
Ad infinitum.
-------------------------------------------------------------------------
Here is some poorly written code I have written to do the job:
<?php
// next line places cookie random value in variable $taxfile
$taxfile = $HTTP_COOKIE_VARS["taxfile"];
// next line creates unique file name for saving form data
$newfile = "F" . $taxfile . "txt";
// next three lines test to see if file already exists; form has not been opened by unique cookie holder
if (!file_exists($newfile)){
//open new txt file and format for two form variables $a and $b
fopen($newfile, "w+");
fputs($filepointer, "","");
// or, if form has already been completed by cookie holder,
}else{
// open file that already exists
fopen($newfile, "w+");
//read contents of file
fread($filepointer,(int filesize(string $newfile));
// how do i put contents into $a and $b?
}
?>
<HEAD>
<TITLE> </TITLE>
<META>
</HEAD>
<BODY>
<form>
<input type="text" name="$a" size="5">
<p>
<input type="text" name="$b" size="">
</form>
<?php
// save $a and $b back to flat file $newfile and close file
fputs($filepointer, "$a","$b");
fclose($newfile);
?>
</BODY>
I don't know how to read the file contents into the form; whether to create and format a file like we would in DOS to receive the data; and how to save data back to the file. I would really appreciate some help.
Jerry Wing