Becky has asked for the wisdom of the Perl Monks concerning the following question:

I have a web form with a drop down menu which allows the user to select multiple items from a list. How do I parse the results? For example:
<select multiple size="5" name="genbank_info"> <option value="genbank_none" selected>none</option> <option value="gi_number">gi number</option> <option value="protein_seq">protein sequence</option> <option value="dna_seq">DNA sequence</option> <option value="genbank">genbank file</option> <option value="locus_link">locus link</option> </select>
If the user selects "gi_number" and "protein_seq", how do I do something with BOTH these values? I can only seem to get access to the last selected value at the moment because all I know how to do is something like
if ($FORM{'genbank_info'} eq "gi_number"){ $gi_number = "1234"; }
but obviously a string can't equal 2 things at once.

Thanks!

Replies are listed 'Best First'.
Re: multiple drop-down menus
by mirod (Canon) on Apr 17, 2003 at 10:12 UTC

    You don't specify how the %FORM hash is filled, so it is quite hard to answer you.

    I will assume you are using CGI.pm, if you are not, then you should! From perldoc CGI:

    FETCHING THE VALUE OR VALUES OF A SINGLE NAMED PARAMETER:

      @values = $query->param('foo');

    -or-

      $value = $query->param('foo');

    Pass the param() method a single argument to fetch the value of the named parameter. If the parameter is multivalued (e.g. from multiple selections in a scrolling list), you can ask to receive an array. Otherwise the method will return a single value.

    This seems to suggest that you should use the first form, @values = $query->param('foo'); to get the values. You can then use the values as the keys to a hash and test on the existence of the key:

    #when you create the FORM hash my @genbank_info= $query->param('genbank_info'); my %genbank_info= map { $_ => 1 } @genbank_info; # create a hash selec +ted_value => 1 $FORM{genbank_info}= \%genbank_info; ... # later... if ($FORM{genbank_info}->{gi_number}) { $gi_number = "1234"; }
Re: multiple drop-down menus
by PodMaster (Abbot) on Apr 17, 2003 at 10:03 UTC