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

I have this selection for my CGI that goes into cgi mailer script where it emails me the value. It works but I want it to give the ability to choose more than one selection so it gives me the ability to select more than one and pass the values onto my cgi script where I am using the "use CGI" module. So if I wanted to select two or three options it will let me do it on the drop down. Here is the html page:
<TR> <TD ALIGN="left"><strong>Choose data:</strong></td> <td> <select name="collab" size=1> <option> <option> First Selection <option> Second Selection <option> Third Selection <option> Fourth Selection </select> </td>

Replies are listed 'Best First'.
Re: Selecting more than one option on form.
by davorg (Chancellor) on Nov 06, 2002 at 14:21 UTC

    The "select" tag has a "multiple" attribute that allows you to select more than one entry.

    <tr> <td align="left"><strong>Choose data:</strong></td> <td> <select name="collab" size="1" multiple="multiple"> <option /> <option> First Selection</option> <option> Second Selection</option> <option> Third Selection</option> <option> Fourth Selection</option> </select> </td>

    In cases like this, CGI::param returns a list of all the selected items.

    --
    <http://www.dave.org.uk>

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

      Anonymonk should note that davorg is super++ because adding multiple="multiple" will make the code XHTML compliant, and who doesn't want that? :)

      John J Reiser
      newrisedesigns.com

      Thanks but the output in my email is still one value. Here is how I am using param in my cgi:
      use CGI qw(:standard); # bundle up form submissions into a mail_body $mail_body = ''; foreach $field (param) { foreach $value (param($field)) { $mail_body .= "$field: $value\n"; } } open (MAIL,"|$sendmail") || die "Can't open sendmail pipe: $!\n"; print MAIL <<"EOF"; To: You From: Me Subject: web data $mail_body EOF
Re: Selecting more than one option on form.
by dingus (Friar) on Nov 06, 2002 at 15:48 UTC
    See the classic Lincoln Stein examples such as this one with text at monty.txt run at monty.cgi.
    print $query->scrolling_list( -name=>'possessions', -Values=>['A Coconut','A Grail','An Icon', 'A Sword','A Ticket'], -size=>5, -multiple=>'true');

    Dingus


    Enter any 47-digit prime number to continue.
Re: (nrd) Selecting more than one option on form.
by newrisedesigns (Curate) on Nov 06, 2002 at 14:24 UTC

    Change the size="1" to something other than one.

    Do this using CGI.pm's functions.

    print scrolling_list(-name=>"collab", -values=>&#91;"First", "Second", "Third", "Fourth"&#93;, -multiple=>'true',);

    The multiple will allow for multiple selections.

    John J Reiser
    newrisedesigns.com

    Update: added "multiple" cause I was a dummy.

Re: Selecting more than one option on form.
by jdporter (Paladin) on Nov 06, 2002 at 14:22 UTC
    You do this by including the word "multiple" in the attribute list for the "select" tag, i.e.     <select name="collab" size=1 multiple> However, most browsers will not render this as a drop-down menu, but as a scrolling list kind of menu. You, as web page writer, shouldn't care, though....
Re: Selecting more than one option on form.
by erasei (Pilgrim) on Nov 06, 2002 at 14:25 UTC
    You need to add the multiple flag to your select tag. Like this:
    <select name="collab" multiple size=1>

    All of the items selected will be passed as an array to your script. So the cgi param collab will be an array, with each slice being one of the values selected from the menu. Also of note, by adding the multiple tag, your html will be rendered as a box, not as a drop down menu.