jeffa++ for using CGI and presenting the right way to go about it. Just to point out however, jeffa's actual code will not work as you might hope. It took me a second to see why he was using map{} to pass the values. Then I realized that the hash was reversed in the way CGI likes to receive it. So the map works correctly. But unfortunately, CGI is not smart enough to take \%fish for the labels and flip the hash around to get the values right.
There are two ways (that I know of) to go about fixing this. I will post both solutions. (As well, I'm hoping jeffa would plan on calling header() before start_html() :) ) First method of getting it done: attach a map{} to the passing of -labels as well. So you get the following:
#!/usr/bin/perl -w use strict; use warnings; use CGI::Pretty qw(:standard); my %fish = ( "Fresh water fish" => "fish1.jpg", "Salt water fish" => "fish2.jpg", "One fish" => "seuss1.jpg", "Two fish" => "seuss2.jpg", "Thanks for all the fish" => "adams.jpg", ); print header(), start_form(), popup_menu( -name => 'fish', -values => [map { $fish{$_} } sort keys %fish], -labels => {map { $fish{$_}, $_ } sort keys %fish} ), end_form();
The second way to get it done will make things much easier and will make it a little more readable. Simply flip the keys and values in the hash around and then the calls to popup_menu() will be quite simple:
#!/usr/bin/perl -w use strict; use warnings; use CGI::Pretty qw(:standard); my %fish = ( 'fish1.jpg' => 'Fresh water fish', 'fish2.jpg' => 'Salt water fish', 'seuss1.jpg' => 'One fish', 'seuss2.jpg' => 'Two fish', 'adams.jpg' => 'Thanks for all the fish' ); print header(), start_form(), popup_menu( -name => 'fish', -values => [sort keys %fish], -labels => \%fish ), end_form();
And from the above two snippets, there should really only be one difference: the first one will sort based on the user-viewed values (ie: 'Thanks for all the fish'), whereas the second snippet sorts based on the actual field values (ie: 'adams.jpg').
C:\>shutdown -s
>> Could not shut down computer:
>> Microsoft is logged in remotely.
In reply to Re^3: Drop-down list Option Value
by Coruscate
in thread Drop-down list Option Value
by Happyjack
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |