?

Search Help Board

PHP FAQ
PHP Articles
PHP Help
Bulletin Board

PHP Manual (NEW!)
First Time PHP'ers
Help with programming
Sql assignment help
PHP Homework Help


C# Help

?
?missing variables when writing to a file
Author:??(---.i-55.com)
Date:???07-04-03 12:34

I have the following script that takes form input, displays it in the same browser window for confirmation, then also writes it to a separate file.

The confirmation window works fine. However, I can't figure out how to call the variable names in order to write them to the file.




Remote Confirmation Display




Remote Confirmation Display























Line 1:
Line 2:
Line 3:
Line 4:
Line 5:
Line 6/VIP:




Notes:













Remote Caller Display


Caller Info
























Line 1:
Line 2:
Line 3:
Line 4:
Line 5:
Line 6:





Notes:




"); ?>



-----

Here's the html form I use for input:


Remote Caller Display Editor


Caller Info










































Line 1 Name:
Subject:
Line 2 Name:
Subject:
Line 3 Name:
Subject:
Line 4 Name:
Subject:
Line 5 Name:
Subject:
Line 6/VIP Name:
Occupation:
Notes:

?Re: missing variables when writing to a file
Author:??(---.cpe.net.cable.roger)
Date:???07-06-03 07:52

why are you writng all the html to the file? there really is no need to do so...better and smaller to store the file as simple comma separated variables...

see ex for this:

/*
this file will take the data from the form and create a file for that person
or if they have a file already will append the data to the file on the last line

*/
//
//***********************************main code block ***************************************************
if ($_POST['submit']){ //check to see if the form have been submitted via the button
write_file_data(); //calls the write_file_data function to enter the data into the file
}else{
show_form(); //show the form to the user and ask for the info.
}
//***********************************end main code block ***********************************************
/*
now the functions are created
first the show_form
*/
function show_form()
{
/* NOTES:

separating the drivers names into 2 parts allows the joining of those parts without spaces
file names with spaces can often cause trouble depending on what server environment and OS
the application is running on

should proably put some clientside javascript validation to ensure that all fields are filled
out but never rely on that as js is easy to get around and some people may have turned js off
on their machines

Dates on forms are much better handled through the use of three drop down boxes for day/month/year
It makes handling the data easier and the formatting to the required / desired date format simpler
I won't change it here

so here we ask the driver for the name (in a real world app we would really do this with a login
or cookie type id depending on the security needs of the application
other variables:
date
cost
cost/litre
total litres entered
*/
echo "
Reading text files























Vehicle logbook

Enter driver's first name
Enter driver's last name
Enter date
Enter total cost to fill up
Enter cost per litre in cents
Enter how many litres put into the vehicle
Enter how many kilometers driven since last fill up



";

} //end function show_form()
//
function write_file_data()
{
/*
first we will retreive all the data from the form and validate it
then we can open the file and write the data
*/
//check the data
if (!empty($_POST['fname']))
{
$first=$_POST['fname'];
}else{
$formError=1;
}
if(!empty($_POST['lname']))
{
$last=$_POST['lname'];
}else{
$formError=1;
}

if((!empty($_POST['total_cost']))&&(is_numeric($_POST['total_cost'])))
{
$total_cost=$_POST['total_cost'];
}else{
$formError=1;
}
if((!empty($_POST['litre_cost']))&&(is_numeric($_POST['litre_cost'])))
{
$litre_cost=$_POST['litre_cost'];
}else{
$formError=1;
}

if((!empty($_POST['numOfLitres']))&&(is_numeric($_POST['numOfLitres'])))
{
$numlitres=$_POST['numOfLitres'];
}else{
$formError=1;
}

if((!empty($_POST['kilometers']))&&(is_numeric($_POST['kilometers'])))
{
$k=$_POST['kilometers'];
}else{
$formError=1;
}
//this will not validate the date
//the best way to validate the date is to use regex expressions - read the manual
if($_POST['date']!="YYYY/MM/DD")
{
$date=$_POST['adate'];
}
if ($formError==1){
//there is some error in the data supplied
//can add code to tell the user there is a problem
show_form();
}else{
/*
Note:
the data is now valiated (sort of) for any text field that has text it is best to do a search and
replace on any apostrophe to prevent sql injection attacks on the db - this is done by replacing any
single apostrophe with two single apostrophies...

*/

$first=str_replace("'","''",$first);
$last=str_replace("'","''",$last);

/*
never open any server objects until you have to and close them immediately afterwards, this prevents
a hogging of server resources and increases the speed of the application since there are not too many
objects in memory at once
*/

$filename=trim($first)."_".trim($last).".txt"; //set the filename to be used by using the combined
//first and last names separated with an underscore
//to avoid naming convention problems

//create the data line to be appended to the file
//use the period as the concatenation operator and combine the data in to one
//CSV line
//the "\r\n" characters at the end create a carriage return and new line
//which is the basis for line separation when reading in a file with the
//file() command

$line=$date.",".$total_cost.",".$litre_cost.",".$numlitres.",".$k."\r\n";


//let's open the file and write the data to it
if (!$file=fopen($filename,"a")){
echo "Could not open file.";
}else{
fwrite($file, $line); //write the data to the file
fclose($file); //close the file
echo "File updated successfully

"; //could consider some error checking to see if the
//fwrite was successful.
//also requires some link to goto another page
//or present more options
echo "Another entry?
See results";

} //end the if then statement
} //close main if then statement
} //close the function
?>

?Re: missing variables when writing to a file
Author:??(---.i-55.com)
Date:???07-07-03 03:15

I write the "extra" html to the file because I want a real-time update of the form information presented to another user after the first user enters the data.

Notice the automatic refresh in the file data.

User B is constantly monitoring the output page. Anytime user A changes data in the from, within 10 seconds the data will be refreshed on the output page.

Go to Top??|??Go to Topic??|??Threaded View??|??Search?
??|??
New Topic
?Reply To This Message
?Your Name:
?Your Email:
?Subject:
Email replies to this thread, to the address above.
??

Provided By
Phorum