in reply to Beginning Perl and Forms
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.#!/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;
Whenever you write a CGI program, you'll hear experienced programmers say the following:
Incidentally, the code snippet I wrote duplicated your code's functionality, but has a slight flaw. The following is a valid query string:
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):color=red&color=blue&color=some%20other%20value
That works because of the following:foreach my $name ( @names ) { my @values = param( $name ); print "Name = $name, Value(s) = " , join (',' @values) , "<br>\n"; }
# 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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: (Ovid) Re: Beginning Perl and Forms - -w Swtich
by Maclir (Curate) on Jan 19, 2001 at 03:32 UTC | |
|