I am trying to append the results from a form to a text file. My code is supposed to print out the results on one page, and append the results to another page each time data is entered on the form and the submit button is hit. Unfortunately, it is only printing out the message acknowledging the submit button–no data on either form. Here is the code below, please help if you can.
PHP code that reads the form submission:
[code]<?php
function WriteToFile ($name, $worth) {
$TheFile = “results.txt”;
$Open = fopen ($TheFile, “a”);
if ($Open) {
fwrite ($Open, “$name: $worth
n”);
fclose ($Open);
$Worked = TRUE;
} else {
$Worked = FALSE;
}
return $Worked;
}
for ($n=0; $n < count ($_POST); $n++){
$line = each($_POST);
$line[key] = $name;
$line[value] = $worth;
print ("$name: $worth
n");
$CallFunction = WriteToFile ($name, $worth);
if ($CallFunction) {
print (“Your submission has been received!
n”);
} else {
print (“Your submission was not processed due to a system error!
n”);
}
}
?>[/code]
PHP code for form:
[code]
<?php $field=array(); $type=array(); $lines = file('test_read.txt'); foreach ($lines as $line) { list($mytype, $name, $option) = explode("=", $line); $name = htmlentities(trim($name)); if(isset($option)) $field[$name][]=htmlentities(trim($option)); else $field[$name]='nooptions'; $type[$name]=trim($mytype); } foreach ($field as $name => $options) { switch($type[$name]) { case 'title': print ("$name |
n"); break; case 'label': print ("
$name
n");
break;
case 'checkbox':
print ("$namen");
break;
case 'radio_button':
print ("
$name
n");
foreach($options as $value)
print ("$valuen");
break;
case 'comment_box':
print ("
$name
n");
print ("
n"); break; case 'textbox': print ("
$name: n"); break; case 'dropbox': print ("
$name: n"); foreach($options as $value) print ("$valuen"); print (""); break; } } print ("
n"); print ("n"); ?> [/code]
Text file that form code reads:
title = Test Form
label = Choose One of These
checkbox = shoe
checkbox = hair
radio_button = Choose One = 3
radio_button = Choose One = 6
comment_box = Comments
dropbox = Select = 18-29
dropbox = Select = 30-49
dropbox = Select = 50-75
textbox = Please type your name