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

I have a selection box with multiple elements listed. The user can hold Control or click-and-drag to select multiple items in the box. My *gasp* homerolled POST form handler only returns the last element in the list that they selected.

After looking through the POD for CGI.pm, I can't seem to find where you can receive multiple values for a certain field.

Can this be done? Should I be looking for an alternative method?

thanks

  • Comment on Multiple Data Selections per Field in CGI

Replies are listed 'Best First'.
(jeffa) Re: Multiple Data Selections per Field in CGI
by jeffa (Bishop) on Jan 28, 2001 at 04:28 UTC
    I would like to say just use CGI and be done with it. But that's too easy. I'd rather talk about the problem at hand.

    So you are only getting the last element, eh? This leads me to believe that you are using something like this:

    split up query string and decode pairs into $val and $key insert $key into params hash with value $val
    That doesn't work when you have a query string like
    foo.cgi?foo=mashed&foo=fried&foo=boiled&foo=tofu
    because foo will be equal to 'tofu' - what you need to do is first check if the key exists, if it does then treat it like an array.

    But there's a catch - if the key exists, then is the value a scalar, or is it an array? If it's the first time the key is encountered, then the value is simply a scalar. The second time the key is encountered, it's still a scalar. The value needs to be converted to an array, and then the new value is added. Each time after that, simply push the new value to the already existing array:

    if (defined $params{$key} && ref($params{$key}) eq "ARRAY") { # key has been seen at least twice now # the value is a an anonymous array, push the new value push (@{$params{$key}}, $val); } elsif (defined($params{$key})) { # second time key has been seen # the value is a scalar, 'turn' it into an anonymous array $params{$key} = [$params{$key}, $val]; } else { # first time this key has been seen # assign the value as a scalar $params{$key} = $val; }
    I learned this trick from maverick many months ago (we both use CGI.pm now, for the record). I only show this in hopes to promote the learning of anonymous data structures and advocate using CGI.pm instead. :)

    Jeff

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    F--F--F--F--F--F--F--F--
    (the triplet paradiddle)
    

      If the key exists, then is the value a scalar, or is it an array? If it's the first time the key is encountered, then the value is simply a scalar. The second time the key is encountered, it's still a scalar. The value needs to be converted to an array, and then the new value is added. Each time after that, simply push the new value to the already existing array:

      You could also do this quite nicely with a tie:

      package Quantum::Heisenberg; sub TIESCALAR { bless [], $_[0] } sub STORE { push @{$_[0]}, $_[1] } sub FETCH { my $self = shift; $self->[int rand @$self]; } 1;

      Changing the FETCH method to do what you'd expect is left as an exercise for the reader ;)

      Tony

Re: Multiple Data Selections per Field in CGI
by Trimbach (Curate) on Jan 28, 2001 at 04:25 UTC
    If your multiple selection field is called "field", then do this:
    use CGI qw(:standard); @all_selections = param ('field');
    @all_selections will then magically have all the values selected in your multiple selection field. It's just that simple.

    Gary Blackburn
    Trained Killer