in reply to Generating Select elements with foreach loops

This is one area where CGI.pm's HTML generation functions are really good.

@quantities = ('1','5','10','25','50','100'); my %labels = map { $_ => $_ } @quantities; $labels{1} = 'Individual'; print popup_menu(-name => 'foo', -values => \@quantities, -labels => \%labels, -default => $userRecord{$pack_qty});

The code above gives the following output:

<select name="foo" > <option value="1">Individual</option> <option value="5">5</option> <option value="10">10</option> <option value="25">25</option> <option value="50">50</option> <option value="100">100</option> </select>
--
<http://dave.org.uk>

"The first rule of Perl club is you do not talk about Perl club."
-- Chip Salzenberg

Replies are listed 'Best First'.
Re^2: Generating Select elements with foreach loops
by pickledegg (Novice) on Jul 11, 2006 at 10:36 UTC
    Thats a nice way of doing it, however that just displays the static menu does it not?
    What about comparing to the db hash and settting the value of selected to suit.
    Sorry I'm not lazy, I've just never really used the CGI module in this way.

      Sorry, yes my example wasn't very clear. If -default is set to one of the values, then the correct item in the list is selected. I think you can get the required value from your %userRecord hash.

      If I change the code to:

      print popup_menu(-name => 'foo', -values => \@quantities, -labels => \%labels, -default => 25);

      Then I get the following output:

      <select name="foo" > <option value="1">Individual</option> <option value="5">5</option> <option value="10">10</option> <option selected="selected" value="25">25</option> <option value="50">50</option> <option value="100">100</option> </select>
      --
      <http://dave.org.uk>

      "The first rule of Perl club is you do not talk about Perl club."
      -- Chip Salzenberg

        Many thanks dave, you are most definitely

        'the man'