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

I have searched all my books and perl monks on this topic, and would love some help. I have an HTML form using

<select MULTIPLE name="name"> <option value=1>blah blah <option value=2>blah blah </select>
This is a nice format because it creates a small, scrolling list where several items can be selected. The thing is several identical names get passed equaling different values.

So I get several name="something"'s passed. CGI.pm will get just the first name/value pair when several names are identical with the param (name) method. Can anyone think of a way of getting ALL of the values passed with the same name? I can think of some complicated ways, but I suppose I'm in search of finesse.

Thanks for your help.

"No matter where you go, there you are."

Jeff Pflueger - Struggling Perl knowledge sponge

Replies are listed 'Best First'.
Re: Passing 'SELECT MULTIPLE' parameters and CGI.pm
by chromatic (Archbishop) on Mar 14, 2001 at 01:11 UTC
    Read the parameters in list context:

    my @multi = $q->param('name'); # or foreach my $name ($q->param('name')) { # do something }

    Congratulations on using CGI to handle this -- many people don't, and get this answer utterly, utterly wrong!

(ar0n) Re: Passing 'SELECT MULTIPLE' parameters and CGI.pm
by ar0n (Priest) on Mar 14, 2001 at 01:13 UTC
    From 'perldoc CGI':
    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.

    update: D'oh! Oh well. Btw, CGI::param detects if you want an array or a scalar by checking the wantarray function. Check it out, it's nifty.

    [ar0n]

(sacked) Re: Passing 'SELECT MULTIPLE' parameters and CGI.pm
by sacked (Hermit) on Mar 14, 2001 at 01:13 UTC
    From the CGI.pm docs:
    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.
    So you can use:
    my @names = $query->param('name');
    and then process @names as you wish.

    UPDATE: *doh*, chromatic and ar0n beat me to it! I filed an Editor Request for deletion.

    --sacked
Re: Passing 'SELECT MULTIPLE' parameters and CGI.pm
by jeffpflueger (Beadle) on Mar 14, 2001 at 01:51 UTC
    Thank you....