That can be done using JavaScript.

To select multiple items from a drop-down list in the same form, you have to store the selected items in another list in the form (and provide a facility to delete item from that list). Upon submit, the items in that list need to be selected, otherwise they won't make it into the CGI params. Get a book covering JavaScript, or visit one of the zillion sites covering JavaScript, listboxes and shopping carts.

Anyways, here's how you can do that.

#!/usr/bin/perl use CGI qw(:all); my $js = <<EOH; function addFromList() { var popup = document.getElementById('title'); var olist = document.getElementById('ordered_items'); for (var i = 0; i<popup.options.length;i++) { if(popup.options[i].selected) { var item = popup.options[i].value; // if you want to allow multiple instances // of the same item in your list, remove the // next three lines. for(var j = 0; j<olist.options.length; j++) { if (olist.options[j].value == item) return; } var opt = new Option(item,item,false,false); olist.options[olist.options.length] = opt; } } } function addFromEntry () { var olist = document.getElementById('ordered_items'); var entry = document.getElementById('requesttext'); if(entry.value != '') { var opt = new Option(entry.value,entry.value, false,false); olist.options[olist.options.length] = opt; } } function removeFromList () { var olist = document.getElementById('ordered_items'); for (var i = olist.options.length - 1; i>=0; i--) { if (olist.options[i].selected) { olist.options[i] = null; } } olist.selectedIndex = -1; } function selectAll() { var olist = document.getElementById('ordered_items'); for (var i = 0; i<olist.options.length; i++) { olist.options[i].selected = true; } } EOH print header(), start_html (-script => $js) ; #===================================================================== +========== my @titlelist; open FILE, "<$ENV{'DOCUMENT_ROOT'}/data/vault/titlelist.txt" or die $! +; chomp (@titlelist = <FILE>); close FILE; print br() x 5; print start_form( action=>'/cgi-bin/admin.pl' ); print table( {class=>'user_form'}, Tr( td( {width=>'55%'}, 'Select items' ), td( {width=>'35%'}, popup_menu( -id=> 'title', -name=>'ti +tle', -values=>\@titlelist )), td( {width=>'10%'}, button(-name => 'add', -value => "add +", -onclick => 'addFromList()'))), Tr( td( 'ask for something else if it is not listed on drop do +wn menu' ), td( textfield( -name=>'requesttext',-id => 'requesttext' ) +), td( button(-name => 'request', -value => 'request', -oncli +ck => 'addFromEntry()'))), Tr( td( 'Your Order list'), td( popup_menu(-name => 'ordered_items', -multiple => 1, - +id => 'ordered_items')), td( button(-name => 'delete', -value => 'remove',-onclick +=> 'removeFromList()'))), Tr( td( 'Soemthing to remark on order' ), td( {colspan=>2}, textarea( -name=>'remark', -rows=>6, -c +olumns=>35 ))), Tr( td( a( {href=>'/cgi-bin/show.pl?name=showbook'}, font( {s +ize=>3, color=>'yellow'}, 'Change Order' ))), td( {colspan=>2}, submit( -name => 'Order!', -onclick => +'selectAll()' ))) ); print hidden(-name=>'date', -value=>$date); print hidden(-name=>'host', -value=>$host); print end_form(); print br() x 5; #===================================================================== +========== if(param('ordered_items')) { print h3('Your Order'),ul(map{li($_)} param('ordered_items')); } if(param('remark')) { print h3('Your remark'),param('remark'); } print end_html; __END__

I would rather use the POST method with that form.

If you don't want to use JavaScript, you will have to use a list box with multiple select turned on (not a drop down); the third possibility is to have your users submit the form after each select, and store the selected items somewhere on the server side, e.g. in a CGI::Session object.

update: I see you've implemented this at your server... as easy as cut-n-paste, no? But come on. Read the code before you include it, and include only as much as makes sense:

You included the whole bunch from the line my $js = <<EOH; so you have the line

print header(), start_html (-script => $js) ;
in it. Which means

Also, you could set the ordered-items list box to some initial width (via css), the small thingy in there just looks funny :-)

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

In reply to Re: How to select many times from a drop down menu in the same form by shmem
in thread How to select many times from a drop down menu in the same form by Nik

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.