Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Beginning Perl and Forms

by Stinger (Initiate)
on Jan 19, 2001 at 02:25 UTC ( [id://52858]=perlquestion: print w/replies, xml ) Need Help??

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

I patched this example together as a very simple program that is supposed to take the output from a form and produce another form with the parsed apart form entries. But it only returns a blank form. Am I missing something? NOTE: I am using a GTE sponsered web site and they told me I needed the first 3 lines exactly as shown. At least when I did this the "internal server" error went away and I at least saw a blank form returned. I am an experienced C++ and VB programmer, but Perl is brand new to me. And comments are welcomed.
#!/usr/local/bin/perl use CGI qw(:cgi-lib :standard); print header(); # Geturl.pl # # A little Perl script to read, decode and print the names # and values passed to it from an HTML Form thru CGI. # Get the HTML header, ender, define the page title. require "/cgi-local"; # full path ? to what ? # print "Content-type: text/html\n\n"; # another GTE addin $Title = "Get Information From a URL"; # Get the query string # $QueryString = $ENV{'QUERY_STRING'}; # Use split to make an array of name-value pairs broken at # the ampersand char. @NameValuePairs = split (/&/, $QueryString); # Put up an HTML header, page title, and a rule. &HTML_Header ($Title); print "<BODY>\n"; print "<H1>$Title</H1>\n"; print "<HR>\n"; # Split each of the name-value pairs and print them on the page. foreach $NameValue (@NameValuePairs) { ($Name, $Value) = split (/=/, $NameValue); print "Name = $Name, Value = $Value<BR>\n"; } &HTML_Ender;

Replies are listed 'Best First'.
(Ovid) Re: Beginning Perl and Forms
by Ovid (Cardinal) on Jan 19, 2001 at 02:53 UTC
    Here's the way I would duplicate the functionality that you are trying to achieve:
    #!/usr/local/bin/perl -wT use strict; use CGI qw/:standard/; # Geturl.pl # A little Perl script to read, decode and print the names # and values passed to it from an HTML Form thru CGI. # Get the HTML header, ender, define the page title. my $Title = "Get Information From a URL"; print header(), start_html ( -title => $Title ), h1( $Title ), hr(); # This gets the name of all of the form elements my @names = param(); # The param() function takes the name of the parameter and returns its + value foreach my $name ( @names ) { print "Name = $name, Value = " . param( $name ) . "<br>\n"; } print end_html;
    Since you are new to Perl, there are quite a few issues that should be covered. If you want to see some of the basics, you can check out my online Web programming course. That should point you in the right direction. Also, you'll want to read about perl security.

    Whenever you write a CGI program, you'll hear experienced programmers say the following:

    • Turn on warnings (that's the -w switch on the shebang line)
    • Turn on taint mode (-T switch. See perlsec)
    • Use strict.
    • Don't try to parse form data by hand. See use CGI or die;. My apologies for the harsh title :)
    Those are some of the basic issues. There's a lot of ground to cover. If you have questions after that, let us know.

    Incidentally, the code snippet I wrote duplicated your code's functionality, but has a slight flaw. The following is a valid query string:

    color=red&color=blue&color=some%20other%20value
    Note that there are three values for 'color'. This is common. My code above will only return the first value entered. I did that to simplify the code. The following will correct for that (rough hack follows):
    foreach my $name ( @names ) { my @values = param( $name ); print "Name = $name, Value(s) = " , join (',' @values) , "<br>\n"; }
    That works because of the following:
    # Assign param() to a scalar and you only get the first value my $value = param( $name ); # Assign param() to an array and you get all of the values my @values = param( $name );

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      Ovid's advice:
      Turn on taint mode (-T switch. See perlsec)
      is generally sound advice. However, if your cgi environment involves running under mod_perl, you will find this causes a warning message. Basically, you cannot enable taint mode under mod_perl with the -T switch on the shebank line.

      From the mod_perl doco:

      Since the -T switch doesn't have an equivalent perl variable, mod_perl provides the PerlTaintCheck directive to turn on taint checks. In httpd.conf, enable this mode with:

      PerlTaintCheck On

      Now any code compiled inside httpd will be taint checked.

      If you use the -T switch, Perl will warn you that you should use the PerlTaintCheck configuration directive and will otherwise ignore it.

      Now, since you are running under an enviroment that is outside your control, you may wish to check whether mod_perl is in use, and if so, whether the PerlTaintCheck directive is in place.

      A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Beginning Perl and Forms
by j.a.p.h. (Pilgrim) on Jan 19, 2001 at 02:27 UTC
    Please put <code></code> tags around your code to make it readable.
      Thanks, this is an HTML enabled window, correct?

      I resubmitted it with the code tags.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://52858]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-03-28 19:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found