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

I have a cgi page with the following information request.. I'm really new to perl and I'm trying to get a feel on what the code is doing.
<form method="post" action="http://test/cgi-bin/write.cgi"> First Number: <input type="text" size="10" maxlength="4" name="no1"> Second Number: <input type="text" size="10" maxlength="1" name="no2"> Third Number: <input type="text" size="10" maxlength="1" name="no3"> Forth Number: <input type="text" size="10" maxlength="8" name="no4"> <INPUT type="SUBMIT" value="Submit">
And the write.cgi file is like so.
#!/usr/bin/perl print "Content-type:text/html\n\n"; 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; $FORM{$name} = $value; } print "<html><head><title>Form Output</title></head><body>"; print "<h2>Results from FORM post</h2>\n"; foreach $key (keys(%FORM)) { print " $FORM{$key}<br>"; print"$key<br>"; } print "</body></html>";
My question is how can I output each number passed to the write.cgi rather than having the array $FORM print it?

Replies are listed 'Best First'.
Re: cgi input box process script
by Errto (Vicar) on Sep 22, 2004 at 01:53 UTC

    You should be using CGI. Your code then becomes something like:

    #!/usr/bin/perl use strict; use warnings; use CGI; my $q = CGI->new; print $q->header("text/html"); print "<html><head><title>Form Output</title></head><body>"; print "<h2>Results from FORM post</h2>\n"; foreach my $key ($q->param) { print $q->param($key) . "<br>"; } print "</body></html>";

    Note that this assumes that a given parameter name appears only once in your form.

Re: cgi input box process script
by bradcathey (Prior) on Sep 22, 2004 at 02:04 UTC

    Check out this classic, Use CGI or die by one of the legends around here.


    —Brad
    "Don't ever take a fence down until you know the reason it was put up." G. K. Chesterton
Re: cgi input box process script
by TheEnigma (Pilgrim) on Sep 22, 2004 at 01:50 UTC
    First of all, %FORM is a hash. It's storing the contents of the form as key/value pairs, with $key equal to "no1", "no2", etc., and $value equal to the number entered in the corresponding text box.

    I'm not sure what you mean by 'output', since that is what write.cgi is doing; it's displaying the values on a web page, via the print statements.

    If you could be more specific, we could help you easier.

    TheEnigma

Re: cgi input box process script
by TedPride (Priest) on Sep 22, 2004 at 07:52 UTC
    You probably want to print only those fields that follow a certain format (no1, no2, etc. for names) and contain data, right? Code for that is as follows:
    foreach(sort(keys(%FORM))) { print $_ . ': ' . $FORM{$_} . "\n" if ($_ =~ /^no\d+$/ && $FORM{$_ +}); }
    Beyond that, some clarification of what you mean might be necessary. The data has to go into a hash regardless of what method you use.