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


-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to