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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.