Re: setting Check box

From: Date: Wed, 28 Mar 2001 01:58:07 +0000
Subject: Re: setting Check box
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-45809@lists.php.net to get a copy of this message
On Wed, 28 Mar 2001 10:18, S. Efurd wrote: > This worked for me as well, and helped me with a problem I was having! > > How would you show what option was selected in a drop down box. I have > a form the user fills out, this goes to a confirmation page to ensure > the data enetered is correct. I have a drop down box for states, and I > cant seem to make it show the state selected it always defaults to > first choice? > > Thanks I've built a function for displaying drop down lists: yours for free. Oh look, Ive even bothered to comment it ;-) It will show selected item[s] Somebody will no doubt post improvements. <?php /* formDropDown - create an HTML <SELECT> * vars: $name - the form variable NAME * $value - the SELECTED option which may be an array for multiple selected items * $labels - assoc. array, list of values=>labels * $multiple - if non-empty, select multiple * $size - number of rows to show if multiple * returns: string, HTML (i.e. for use in echo or print statement) */ function formDropDown($name,$value,$labels,$multiple = '',$size ='') { $html = '<SELECT NAME="' .$name . '"'; if(!empty($multiple)) { $html .= ' MULTIPLE'; } if(!empty($size)) { $html .= ' SIZE=' . $size; } $html .= ">\n"; $key = key($labels); while($key != "") { /* Check if the current value is in the $value variable */ if(is_array($value)) { if(in_array($key,$value)) { $selected = ' SELECTED'; } else { $selected = ''; } } else { if ($key == $value) { $selected = ' SELECTED'; } else { $selected = ''; } } $html .= "<OPTION VALUE=\"$key\"$selected>$labels[$key]\n"; next($labels); $key = key($labels); } $html .= "</SELECT>\n"; return $html; } ?>

« previous php.general (#45809) next »