Create a Free Website

PERL CGI Tutorial
for Writing Interactive Form Scripts


Parsing an HTML Form

Parsing is simply taking the information received from an HTML form interpreting it and converting it to a usable data.

If this is your first experience with HTML forms you may want to read the lessons on Forms in the HTML tutorial before starting.

This exercise provides you with a simple form that queries a user for information and a second script that parses the information that is collected.

The action statement in the first form sends the information to a second script where it is parsed and processed.

TRY This Website Builder FREE!

No Credit Card Needed!
Create beautiful,professional websites
WITHOUT Learning to Code!!

Use this Next Generation Builder
to make blogs, portfolios or
full blown websites.
No Experience Needed!

SquareSpaceSquareSpace

Start Your Website Now!SquareSpace

Professional websites
Without the HASSLE!



Send the Form to the Browser

The first script sends a simple HTML form to the browser. The user inputs a first and last name and clicks the submit button and sends the information to the parse_form.pl script via the action statement.

form.pl

#!/usr/bin/perl
print "Content-type:text/html\n\n";
print "<html>\n";
print "<head><title>Sample Form</title></head>\n";
print "<body>\n";
print "<form method=POST action=parse_form.pl>\n";
print "<pre>\n";
print "Enter First Name:<input type=text name=first size=20>\n";
print "Enter Last Name:<input type=text name=last size=20>\n";
print "<input type=submit value=Submit><input type=reset>\n";
print "</pre>\n";
print "</form>\n";
print "</body>\n";
print "</html>\n";


Try the script

Parse the Form

The action statement in the form above specifies a script named parse_form.pl. This CGI script will convert the information received from the form into usable data and send it to the browser in the form of an HTML page.

The first block of code is fairly universal. You can use it to parse just about any form using the POST method. The last 2 lines would be changed to coincide with the input box names on the form.


parse_form.pl

#!/usr/bin/perl
read(STDIN, $buffer,$ENV{'CONTENT_LENGTH'});
$buffer =~ tr/+/ /;
$buffer =~ s/\r/ /g;
$buffer =~ s/\n/ /g;
$buffer =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/eg;
$buffer =~ s/<!--(.|\n)*-->/ /g;
$buffer =~ tr/\\|[|]|<|!|"|$|{|}|*|#|'|>|||;|%/ /;
@pairs = split(/&/,$buffer);
foreach $pair(@pairs){
($key,$value)=split(/=/,$pair);
$formdata{$key}.="$value";
}
$first=$formdata{'first'};
$last=$formdata{'last'};

print "Content-type:text/html\n\n";
print "<html>\n";
print "<head><title>Sample Response</title></head>\n";
print "<body>\n";
print "<p>Thank you: $first $last</p>\n";
print "</body>\n";
print "</html>\n";

Helpful Information

Windows users: Copy the code for each script and paste it into Notepad. Use the Saveas function to save the scripts with a .pl extension. Upload them to your cgi-bin folder and use the Chmod function to change the permissions for each script to 755.

Run the form.pl script from your browser by typing yourdomain/cgi-bin/form.pl

If the scripts don't run and you've checked the shebang line and set file permissions see: Obscure Errors