CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Pull Down w/Multiple Selections

 



tyrant
Deleted

Jan 16, 2001, 12:57 PM

Post #1 of 2 (403 views)
Pull Down w/Multiple Selections Can't Post

I am in the process of writing a script in which the user will be presented with choice of viewing multiple results for some previously collected data.

I had originally done this as a check box option, but with almost 70 options to choose from, the screen has gotten quite cluttered.

I would like to do this with a pull down menu with the option of choosing multiple selections. I have been able to successfully integrate the pull down menu into my script, but when the user clicks submit, only the last selection in the list which has been chosen is passed along to the next iteration.

Any suggestions?



Jasmine
Administrator

Jan 16, 2001, 2:42 PM

Post #2 of 2 (399 views)
Re: Pull Down w/Multiple Selections [In reply to] Can't Post

The easiest way to handle select multiple fields (and forms in general) is by using CGI.pm.

First, the code:

Code
use CGI; 

my $in = new CGI;

print $in->header();

%fieldlabels = (
0 => 'zero',
1 => 'one',
2 => 'two',
3 => 'three',
4 => 'four',
5 => 'five',
6 => 'six',
7 => 'seven',
8 => 'eight',
9 => 'nine',
);

@fieldvalues = keys %fieldlabels;

print "Hold down the control key to select multiple values\n";

print $in->start_form();

print $in->scrolling_list(
-name =>"test",
-values =>[@fieldvalues],
-default =>[2,4,5],
-size => 5,
-multiple=> 1,
-labels => \%fieldlabels,
);
print $in->submit();

print $in->end_form();

print "The multiple values that were selected were:\n\t";

my @testvalues = $in->param('test');

print join "\n\t", @testvalues;


Now, the explanation (notes precede the code they're explaining):

This both initializes CGI.pm and parses form input. The values of user input is "stored" in $in->param, and can be accessed by passing individual field names to param, such as $in->param('test'):

Code
use CGI; 
my $in = new CGI;

Output the content-type line.

Code
print $in->header();

Now here's where the fun starts. Create a hash of all options for the select multiple field, entering the value as the key, and the label as its value.

Code
%fieldlabels = ( 
0 => 'zero',
1 => 'one',
2 => 'two',
3 => 'three',
4 => 'four',
5 => 'five',
6 => 'six',
7 => 'seven',
8 => 'eight',
9 => 'nine',
);

From the %fieldlabels hash, grab the values (which are the keys). This will be used when constructing the select multiple form field.

Code
@fieldvalues = keys %fieldlabels;

Offer instructions to user.

Code
print "Hold down the control key to select multiple values\n";

Insert the <FORM> field

Code
print $in->start_form();

Enter all of the fields you want in your form. Then for the select multiple scrolling list, you can use CGI.pm's scrolling_list function. Note that function call starts with $in-> which directly relates to the my $in = new CGI; constructor. This makes the form "sticky", meaning if you have the scrolling list on the next page, the values that the user defines here will be remembered and preselected.

Code
print $in->scrolling_list( 
-name =>"test",
#the name of your select field
-values =>[@fieldvalues],
#the list of all values
-default =>[2,4,5],
# default values, if any
-size => 5,
# how many rows you want to appear in the scrolling list
-multiple=> 1,
# permit multiple selections (leave out for single selection)
-labels => \%labels,
# pass hash reference of labels.
# If not passed, will use values as labels
);

Print a submit button

Code
print $in->submit();

Print the </FORM>

Code
print $in->end_form();

Useless message.

Code
print "The selected values were:\n\t";

Because the select multiple has multiple options, it is returned in an array. So you can access the selected values for (select multiple) field named "test" just by assigning the value of the input into an array.

Code
my @testvalues = $in->param('test');

Prints out the selected options

Code
print join "\n\t", @testvalues;

Good luck!


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives