Using arrays for forms

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:

[code]<?php
$lines = file(‘test_read.txt’);
//print (“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">$valuen");
        break;

    case 'radio_button':
    	//print ("<p>Choose one:<br>n");
        print ("<input type="radio" name="userbox" value="$value">$valuen");
        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 ("");
break;

		}
}

}
print (“

n”);
?>[/code]

checkboxes are used to make a “Choose any of these” distinction.

what u are looking for are radio buttons.

anyway to make the radio buttons groups together u need a second value (like with select and option). one that provides a name for all items and a second one that keeps the value for everyone of them.

how u wanna do that is up to u. What i would do is splitting the data for dropdowns and radiobuttons in 3 parts.

for the checkboxes i would use a type called lable

title = Test Form
label = Choose at least one of these
checkbox = shoe
checkbox = hair
radio_button = Description = 3
radio_button = Description = 6
comment_box = Comments
dropbox = Label = 18-29
dropbox = Label = 30-49
dropbox = Label = 50-75
textbox = 50

then i would collect them in an two accosiativ array:
(untested example)[php]$field=array();
$type=array();
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 '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="checkbox" 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 (“

n”);
[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service