Jump to content

Posting information from a form to a text file


hotrod57

Recommended Posts

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:

<?php

function WriteToFile ($name, $worth) {
$TheFile = "results.txt";
$Open = fopen ($TheFile, "a");
if ($Open) {
	fwrite ($Open, "$name: $worth<br>\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<br>\n");
$CallFunction = WriteToFile ($name, $worth);
if ($CallFunction) {
	print ("Your submission has been received!<br>\n");
} else {
	print ("Your submission was not processed due to a system error!<br>\n");
}
} 
?>

 

PHP code for form:

<form action="phpform.php" method=POST>
<?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 ("<table><tr><td width=200 align=center><font size=+1>$name</font></table><br>\n");
    		break;

        case 'label': 
            print ("<p>$name<br>\n"); 
            break; 

        case 'checkbox': 
            print ("<input type=\"checkbox\" name=\"$name\" id=\"$name\" value=\"true\"><label for=\"$name\">$name</label>\n"); 
            break; 

        case 'radio_button': 
            print ("<p>$name<br>\n"); 
            foreach($options as $value) 
                 print ("<input type=\"radio\" name=\"$name\" id=\"$name-$value\" value=\"$value\"><label for=\"$name-$value\">$value</label>\n"); 
            break; 
            
        case 'comment_box': 
            print ("<p><label for=\"$name\">$name</label><br>\n"); 
            print ("<p><textarea id=\"$name\" name=\"$name\" rows=\"4\" cols=\"50\"></textarea>\n"); 
           break; 
        	
         case 'textbox': 
           print ("<p><label for=\"$name\">$name: </label><input type=\"text\" id=\"$name\" name=\"$name\">\n"); 
           break; 
           
        case 'dropbox': 
           print ("<p><label for=\"$name\">$name: </label><select name=\"$name\"  id=\"$name\"><option></option>\n"); 
           foreach($options as $value) 
                print ("<option value=\"$value\">$value</option>\n"); 
           print ("</select>"); 
           break; 
          } 
} 
print ("<p><input type=submit name=submit value=\"SUBMIT\"></form>\n");
print ("<input type=reset value=\"Reset\">\n");
?>
</form>

 

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.