Re: Drop down list - persistant value
| From: | Peter H. Lemieux | Date: | Wed, 13 Apr 2005 12:55:39 +0000 |
| Subject: | Re: Drop down list - persistant value | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-213002@lists.php.net to get a copy of this message | ||
I use a function to create select boxes:
function make_select($fieldname,$options,$selected,$opt="") {
# make a select box with option text $opt# $fieldname = name of html field # $options = associative array of select options # $selected = field key of selected item # $opt = optional attributes, e.g., styles, etc.
$output.="\n<select name=\"$fieldname\" $opt >";
foreach ($options as $key=>$val) {
$output.="\n<option value=\"$key\"";
if ($key==$selected) {
$output.=" selected";
}
$output.=">$val";
}
$output.="\n</select>\n";
return $output;
}
By passing the previously selected item as $selected it will automatically reselect the chosen item. For instance,
<?php echo make_select("myfield",$options,$_POST["myfield"]) ?>
I've written similar functions for check boxes and radio buttons. They've made form creation and handling enormously easier.
Peter
Jacques wrote:
I am using PHP. I have a registration page where the user has to select his country form a drop down list. If the username that the user selected for himself exists in the database I am sending him back to the registration form with all the fields completed with the values he previously entered. This is easy to do for a textfield but how do I indicate on the drop down list which country he originally selected? Please help. Jacques