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

Moody Monks,

Mixed html/perl-cgi question.

In an html form users are to select the publishing years for which they want results. It may be any number of years of the last decade and selections may be non=sequential.

I'm looking for options.

Obvious way is with a <select multiple> element. But the client finds that their users dislike having to fiddle with the shift or ctrl key to make selections.

I could make individual check boxes for each year available, then then the perl is going to be ugly as it looks for the presence of checkbox name-value pairs.

if ($form_values{'year_1'}) { push(@years, $form_values{'year_1'})); } if ($form_values{'year_2'}) { push(@years, $form_values{'year_2'})); } .....
Or some variant that cycles through all form elements looking for anything named "year_x".

I've read of, and tried, giving multiple check boxes the same name, hoping for a delimited list of selected values or something, but that's not what I get. Tehr eis conflicting information on the web about whether this should work or not.

What I want is the user simplicity of checkboxes with the server-side handling simplicity of a multiple select.

Any ideas?

Thanks.

Forget that fear of gravity,
Get a little savagery in your life.

Replies are listed 'Best First'.
Re: Multiple form selections in cgi
by sauoq (Abbot) on Sep 26, 2005 at 19:30 UTC

    Uhm... I think you may be overthinking this. All you have to do is give all your checkboxes the same name attribute:

    <input type="checkbox" name="year" value="1996" /> <input type="checkbox" name="year" value="1997" /> . . . <input type="checkbox" name="year" value="2005" />
    Using CGI, all the checked boxes will be available in an array returned by param. Use something similar to:
    my @selected_years = $query->param('year');

    -sauoq
    "My two cents aren't worth a dime.";
    
      Yes! That works perfectly. And exactly the kind of "built-in" functionality I was looking for. Thanks.

      Forget that fear of gravity,
      Get a little savagery in your life.

Re: Multiple form selections in cgi
by ikegami (Patriarch) on Sep 26, 2005 at 18:51 UTC

    The param method, when called with no arguments, returns a list of the names of all the CGI parameters.

    foreach ($cgi->param()) { if (/^year_\d+$/) { push(@years, $cgi->param($_)); } }

    which can be rewritten as follows:

    @years = map { $cgi->param($_) } grep { /^year_\d+$/ } $cgi->param();
      Thanks. This would definitely work, and fall within the definition of

      Or some variant that cycles through all form elements looking for anything named "year_x".

      For the sake of elegance, I'm wondering if there's not something else.

      Forget that fear of gravity,
      Get a little savagery in your life.

        Unfortunately the id attributes of your form elements do not get sent to your CGI scripts, only the names and values.

        One of the features (OK, probably the only feature) that I like about PHP is you can name form elements something like blah[3][2] and then PHP will automatically create an 2D array with that element's value at [3][2]. I wonder if there's a CPAN module for doing similar form parsing in Perl?