in reply to Dynamically marking a HTML select option as selected

It's not overly flexible in terms of layout, but one should mention the CGI.pm solution if only for completeness:

$html .= $query->radio_group(-name => "my_parameter", -values => [qw/option1 option2/], -default => $existing_value, -labels => {option1=>"Option 1", option2=>"Option 2"}, -linebreak => "true");

It seems that CGI.pm uses "checked" instead of "selected", despite that selected is the spec. For more copious options, see your local CGI.pm documentation.

Update: In my sleep-deprived state, I somehow saw "drop-down list" and thought "radio group." Don't ask about the details. Anyways, all of the above does apply to popup_menu as well as radio_button, as mortis points out. Memo to self: Engage brain before noding.

perl -pe '"I lo*`+$^X$\"$]!$/"=~m%(.*)%s;$_=$1;y^`+*^e v^#$&V"+@( NO CARRIER'

Replies are listed 'Best First'.
Re: Re: Dynamically marking a HTML select option as selected
by mortis (Pilgrim) on Dec 19, 2001 at 20:13 UTC
    I applaud you mentioning CGI.pm, it does have a solution - it's just not the radio_group method, it's popup_menu, or scrolling_list, depending on weather you want a single selection drop-down or a multi-value scrolling list box.
    $html .= $query->scrolling_list( -name => 'my_parameter', -values => [qw/option1 option2/], -default => $existing_value, -labels => { option1 => 'Option 1', option2 => 'Option 2'}, -multiple => 'true', ); # or $html .= $query->popup_menu( -name => 'my_parameter', -values => [qw/option1 option2/], -default => $existing_value, -labels => { option1 => 'Option 1', option2 => 'Option 2'}, );
    The other absolutely wonderful thing about CGI.pm is that if the parameter already has a value in the $query object (which you can set explicitly), it will automaticly have it's old value (i.e. it will stick) when you use $query to produce the html (that is if you don't set it explicitly as above). This is great for situations when you have to re-display an HTML page with a form to the user so they can either correct fields, or fill in required fields.

    If you want to have a default value without overwriting the user's entry, an easy thing to do is:

    $query->param('my_parameter',$defaultValue) unless defined $query->p +aram('my_parameter');

    hth