Jump to content

Using arrays for forms


hotrod57

Recommended Posts

I have this code that is supposed to read a text file and print out a form according to the data in the file.  I am using a switch statement to break up the array and separate the values.  what I have found out is that if I have more than one item (e.g. checkbox), the switch doesn't treat them as being in the same category.  For example, if I put a statement like 'Choose one of these:', and I have three checkboxes, I get three 'Choose one of these:' statements on separate lines with checkboxes.  Is there any way I could get that statement printed out only once, but still get my three checkboxes, just like on a regular form?  And I am having problems with my dropbox as well.  Instead of getting the three values in one dropbox, I get three separate dropboxes  with one value in each.  If anyone could help, I would appreciate it.

 

Text File:

title = Test Form
checkbox = shoe
checkbox = hair
radio_button = 3
radio_button = 6
comment_box
dropbox = 18-29 
dropbox = 30-49 
dropbox = 50-75
textbox = 50

 

PHP Code:

<?php
$lines = file('test_read.txt');
//print ("<form action=\"$PHP Form\" method=POST>\n")
foreach ($lines as $line) {
    list($field, $value) = explode("=", $line);

    $value = substr(trim($value), 0, 100);

    switch(trim($field)) {

        case 'checkbox':
        	//print ("<p>Choose one:<br>\n");
            print ("<input type=\"checkbox\" name=\"userbox\" value=\"$value\">$value\n");
            break;

        case 'radio_button':
        	//print ("<p>Choose one:<br>\n");
            print ("<input type=\"radio\" name=\"userbox\" value=\"$value\">$value\n");
            break;
            
        case 'comment_box':
           	print ("<p><textarea name=\"commentbox\" rows=\"4\" cols=\"50\"></textarea>\n");
        	break;
        	
        case 'textbox':
        	print ("<p><input type=\"text\" name=\"textbox\" size=\$value\>\n");
        	break;
        	
        case 'dropbox':
        	print ("<p><select name=dropbox><option>Choose One</option>\n");
        	while ($value) {
		print ("<option value=\"$value\">$value</option>\n");
//			for ($n = 0; $n < count($value); $n++) {
//				while ($n = 1) {
//					$value = $select;
//        	print ("<select name=\"dropbox\" size=\$value\ option value=\"$select\"></select>");
        	break;

    		}
    }
}
print ("<p><input type=submit name=submit value=\"SUBMIT\"></form>\n");
?>

Link to comment
https://forums.phpfreaks.com/topic/75939-using-arrays-for-forms/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.