[lug] Perl CGI Question
Tkil
tkil at scrye.com
Wed Aug 6 15:08:36 MDT 2003
>>>>> "Joey" == Joseph McDonald <joem at uu.net> writes:
Joey> Please forgive the long code paste. I've been racking my brain
Joey> trying to figure out the best way to set an option value
Joey> 'selected' in a select query in a perl CGI. Best demonstrated as
Joey> a short working example..
One way would be to use arrays and hash slices:
my @months = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
my %selected; @selected{ @months } = ( '' ) x @months;
if ( $date ) { $selected{$date} = 'selected' };
foreach my $m ( @months )
{
print qq|<option value="$m" $selected{$m}>$m</option>\n|;
}
Less esoterically, you still want to use a list, but you can build up
the tags more piecemeal:
my @months = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
foreach my $m ( @months )
{
my $attr = qq|value="$m"|;
if ( $m eq $date ) { $attr .= " selected"; }
print qq|<option $attr>$m</option>\n|;
}
Finally, if you use the CGI module, it handles some of this for you.
t.
More information about the LUG
mailing list