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

Hello, thank you very much for your great comments and valued suggestions. I am deeply sorry for not being clear enough on my first attempt. My problem was only to learn why the values I select from my form array are not making it into the text file as they should? The last choice I select is the only value being added to the list at this time. (In the past, I had real name values written in my html form. Passing/controlling these type of values has been so simple.) The below example uses an array made directly from opening a given dir. This confuses me deeply and I have tried so very hard to find the error for weeks... I have again polished things up a bit and followed advice. Sincerely, I don't yet fully understand how to write all the different methods using perl, (I do promise to study them for the benifit they will bring me) if I may request the example or correction to follow my current coding style. Hope I'm not being to forward, Thank you again in advance for your time and insight. Jim
#!/usr/bin/perl -w use CGI::Carp qw(fatalsToBrowser); ############################################# $path_to_files = "/home/foo/www/dir"; $script = "$ENV{'QUERY_STRING'}"; ############################################# read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $value =~ s/\n/ /g;# added to strip line breaks $FORM{$name} = $value; } ############################################# print "Content-type: text/html\n\n"; $query = "$ENV{'QUERY_STRING'}"; if ($query eq "test") {&Display_Form} elsif ($FORM{'create_list'} eq "Create") {&Create_List} else {&Display_Form} #### BEGIN DISPLAY FORM #### sub Top { opendir DIR, "$path_to_files" or die "Can't open directory:$!\n"; @thefiles = sort(grep(/html$/, readdir(DIR))); closedir(DIR); $count = @thefiles; print "<html>"; print "<head><title>MY SIMPLE FILE SELECTOR</title></head>"; print "<body bgcolor =\"#FFFFFF\" text =\"#000000\" link = \"#808080\" + vlink = \"#808080\" alink = \"#808080\">"; print "<center><h2>MY SIMPLE FILE SELECTOR</h2>"; print "<br><br><small><b>The following [$count] files were found in th +e [$path_to_files] Dir.</b></small><br><br>"; print "</center>"; } sub Display_Form { &Top; print "<center><table width=\"246\">"; print "<tr>"; print "<td align=\"left\" width=\"312\">"; print "<form action=\"$script\" method=\"POST\">"; print "<li type=\"square\"><font size=\"2\"><i>Select from list</i></f +ont><BR>"; foreach (@thefiles) { $choice = $_; print "<input type=\"checkbox\" name=\"choice\" value=\"$choice\"><b>$ +choice</b><br>"; } print "<p><input type=\"submit\" name=\"create_list\" value=\"Create\" +></p>"; print "</form>"; print "</td></tr></table>"; &Bottom; } sub Bottom { print "<center><br>"; print "<small><small>$footer</small></small>"; print "</center></html>"; } #### END DISPLAY FORM #### #### BEGIN CREATION PROCESS #### sub Create_List { $choice = "$FORM{'choice'}"; @choices = split(/,/, $choice); $selected = @choices; open FILE,">list.txt" or die "Can't open file:$!\n"; foreach (@choices) { print FILE "$choice\n"; } close FILE; &Finished; } sub Finished { &Top; print "<center><h4>List created</h4>"; print "You selected ($selected):<br><li>$choice</li><br>"; print "<br><A HREF=\"javascript:history.back()\">Go Back...</A></cente +r>"; &Bottom; } #### END PROCESS ####

Replies are listed 'Best First'.
Re: Problem passing selected values to text file
by chromatic (Archbishop) on Oct 14, 2003 at 18:34 UTC
    My problem was only to learn why the values I select from my form array are not making it into the text file as they should? The last choice I select is the only value being added to the list at this time.

    Yes, because your CGI processing code is broken. It took me half a second to find the error (and in ten seconds, I found at least six other errors).

    If you posted this code in your previous question (as your current question indicates), several people would have recommended you use the CGI module for fetching parameters from the user agent. use CGI or die explains the reasons why in sufficient detail.

    If you just want working code, I suggest the following snippet will do what you really want to do instead:

    use CGI; my $q = CGI->new(); my @choices = $q->param( 'choice' );
Re: Problem passing selected values to text file
by Ovid (Cardinal) on Oct 14, 2003 at 19:20 UTC

    As a follow-up to chromatic's comments: if you prefer programming in a functional style rather than an OO one, you can also import CGI.pm methods as functions:

    use CGI ':standard'; my @choices = param('choice');

    You are already familiar with Perl. See my CGI course for more information.

    Cheers,
    Ovid

    New address of my CGI Course.

Re: Problem passing selected values to text file
by Anonymous Monk on Oct 15, 2003 at 12:48 UTC
    The problem is that tha code you currently have to parse the form input
    code: ---------------------------------------------------------------------- +---------- read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); @pairs = split(/&/, $buffer); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $value =~ s/\n/ /g;# added to strip line breaks $FORM{$name} = $value; } ---------------------------------------------------------------------- +----------
    does not handle multiple values. You should use the CGI module instead.
    Replace the code above with code: --------------------------------------------------------------------------------
    use CGI; my $cgi = CGI->new();
    --------------------------------------------------------------------------------
    Then anywhere you would use $FORM{'parameter'} instead use $cgi->param('parameter')
    See http://www.perldoc.com/perl5.8.0/lib/CGI.html for more details.