$html .= qq[<select name="foo">];
for ('option1:One', 'option2:Two') {
my ($val, $desc) = split /:/;
my $sel = ($val eq $existing_value) ? ' selected' : '';
$html .= qq[<option value="$val"$sel>$desc];
}
$html .= qq[</select>];
-- Chip Salzenberg, Free-Floating Agent of Chaos | [reply] [Watch: Dir/Any] [d/l] |
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' | [reply] [Watch: Dir/Any] [d/l] |
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 | [reply] [Watch: Dir/Any] [d/l] [select] |
Quick and dirty way: Just use a regexp to add in the 'selected' string if your default value exists. You may want to do some extra error checking too, like making sure that your default value is a valid option.
my $parameter_value = "option2"; # Your default value from the DB
my $parameter_select = qq(
<select name="parameter">
<option value="option1">Option 1
<option value="option2">Option 2
</select>
); # your select statement string
if ($parameter_value) {
$parameter_select =~ s/value="$paremeter_value"/value="$paremeter_va
+lue" selected/;
}
| [reply] [Watch: Dir/Any] [d/l] |
$html .= qq{<select name="my_parameter">\n};
for my $option ( ['option1', 'Option 1'],
['option2', 'Option 2'],
['option3', 'Apple pie'] ){
if ($option->[0] eq $existing_value){
$html .= qq{<option value="$option->[0]" selected>$option->[1]</opti
+on>\n};
}else{
$html .= qq{<option value="$option->[0]">$option->[1]</option>\n};
}
}
$html .= "</select>\n";
The inside could be rewritten as:
$html .= qq{<option value="$option->[0]"} .
($option->[0] eq $existing_value ?
' selected' : '') .
qq{>$option->[1]</option>\n};
And you should think about using entities, 'cause an $existing_value with a " in it isn't going to make you happy...
2;0 juerd@ouranos:~$ perl -e'undef christmas'
Segmentation fault
2;139 juerd@ouranos:~$
| [reply] [Watch: Dir/Any] [d/l] [select] |