in reply to How to select many times from a drop down menu in the same form
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
in it. Which meansprint header(), start_html (-script => $js) ;
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}
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How to select many times from a drop down menu in the same form
by Nik (Initiate) on May 06, 2007 at 10:19 UTC | |
by shmem (Chancellor) on May 06, 2007 at 10:26 UTC |