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

Hi! I have a variable number of multiple choice select lists each list having each own different values.From each list I select one or more values and send them with a form to a script. In this final script I want to print all the values I selected!Here is the code for the multiple choice lists:
open(FILE, "$data_file") || die("Could not open file: $!"); @param=<FILE>; close<FILE>; foreach $line(@param){ chomp ($line); print "<br><br><br><br><b>The current list of the parame +ter <font color=#FF6600>$line</font></b><hr>\n"; my $i="$line.txt"; my $new_param="/data/aww/cgi-bin/$i"; #for each line re +presents a parameter and for each parameter I open coresponding text +file with it's values print "<table>\n"; print "<select multiple name=\"$line\" size=4>\n"; open(FILE1, "$new_param") || die("Could not open file: + $!"); @val=<FILE1>; close<FILE1>; foreach $newline(@val){ chomp ($newline); print "<option value=\"$newline\">$newline\n"; } print "</select></td></tr></table>\n"; print "<input type=\"hidden\" name=\"$line\" value +=\"$newline\">"; }
In the $data_file I have smth like this:
State Code Server
I don't know how to print all the values selected in another cgi script. Thank you for your time.

Replies are listed 'Best First'.
Re: multiple-choice selects
by eric256 (Parson) on Oct 23, 2003 at 15:25 UTC

    From the CGI docs on CPAN

    @values = $cgi->param('foo');

    That should give you all the selected values for field foo.

    The following will give you a list of all fileds. Then its just a matter of going throuh all the fields and printing out their values.

    my @fields = $cgi->param();

    Finished code might look something like

    my @fields = $cgi->param(); foreach my $field (@fields) { print "Field $field = " . join(",", @{$cgi->param($field)}) . "\ +n"; }

    Thats not very pretty and its untested, but its the general idea. I like to prefix groups of fields with something so that then you can grep just them out later. So you could name each select box param_1,param_2, ect. That way you can include other fields that you want to handler differently. Good Luck!


    ___________
    Eric Hodges
Re: multiple-choice selects
by hmerrill (Friar) on Oct 23, 2003 at 16:24 UTC
    Eric already answered your question with an excellent description of using the CGI 'param' method and accepting the values returned into an array.

    I just wanted to point out something I noticed when looking at your code - I think it's incorrect to do
    close<FILE>;
    So I looked up the 'close' command in the "Programming Perl" book by Wall, Christiansen, and Schwartz - it saws to use close like this:
    close FILE;
    You can also find perldocs on the 'close' function by doing
    perldoc -f close
    at a command prompt. I'm not at all sure what 'close<FILE>' really does.

    HTH.

      also use strict and warnings.. part of your code would break under strict... no serious problems.. but in future larger projects it could make a world of difference.

      I started perl doing CGI, and to look back on my horrible code from my early CGI days now would make me sick. Also something to try coding style wise is use of $_ in foreach loops, it saves a little memory and is kinda slick.

      one of your loops could be re-written as...
      foreach(@val) { chomp; print "<option value=".$_.">".$_."\n"; }

      Make no mistake... none of this will improve/change the funcionality, but I know from personal experience that when working with CGI and new to perl that people are often more concerned with the ends than the means...so this is just some food for thought.