Sending HTML Form with ActivePerl

Basics of ActivePerl - Part 21

Chrys Forcha
Introduction
This is part 21 of my series, Basics of ActivePerl. In this part of the series, we see how an HTML form can be sent from a browser to be received by ActivePerl script in a web server. I show you how the script can obtain the values of the HTML Form controls at the server. Everything said in this tutorial is applicable to traditional Perl.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

A Sample Form
We shall use the following form for illustration:

Sample Form

*First Name
*Email

*Subject
*Message
ClearSend

Save the above code as an HTML file and open it in a browser to see what it looks like. There are five input controls: one for first name; one for sender's email address; one for receiver's email address; one for subject and the last for message. The ActivePerl script to receive the form information is in the cgi-bin directory, and it is called, receive.pl.

Class and Object
We saw in one of the previous parts of the series that a function is a set of statements in a block to carry out a particular sequence of task, repeatedly. That kind of thinking can be extended. Precisely you can have variables and functions in one unit to accomplish a particular task that repeats. Such a unit is called a class. You usually cannot use a class. You have to create (instantiate) what is called an object from it. The object has all the variables and functions that the class has. With the object you can give different values to the variables. So one object can have one set of values for its variables, while another object created from the same class would have a different set of values for its variables. In that way, objects created from the same class can have different (slightly) outcomes.

The CGI Class
ActivePerl has a predefined class called a CGI class. You can create an object from this CGI class. When the dataset from the Browser Form arrives at the server, it is gotten by this object you have created from the CGI class. You can then obtain the values of the controls from the object. You must know the name of the controls in advance; because you have to use the name of a control to in order to obtain the control's value from the object.

You create a new object from the CGI class as follows:

my $query = new CGI;

The name I have given to the object created is $query. You can give any name you want. It is called the query object. new and CGI are reserved words for the class. The syntax to obtain the value from the control name is:

my $value = $query->param('name');

where $value is the variable name that you give for the return value. $query is the variable for the query object. In the syntax, it is followed by ->. Then you have the class reserved word param and parentheses. In the parentheses you have the name of the control whose value you are looking for, in quotes. You need to know the names of the controls in the browser Form before you code the ActivePerl script (especially for the controls that are required).

Before you use the above two syntax, you have to indicate that you want to use the CGI class. This is because the coding of the class is in a file somewhere in the ActivePerl installation. To do this, you have to type the following before the statements corresponding to the above two syntax:

use CGI;

The Script
In this tutorial the name of the ActivePerl script is receive.pl, residing in the cgi-bin directory. It obtains the values of the controls from the CGI object and prints them back to a new page at the browser. This is the script:

use strict;

print "Content-Type: text/html\n\n";

use CGI;
my $query = new CGI;

my $fnVal = $query->param('First Name');
my $emVal = $query->param('Email');
my $reVal = $query->param('Recipient');
my $sbjVal = $query->param('Subject');
my $msgVal = $query->param('Message');

print $fnVal, "
";
print $emVal, "
";
print $reVal, "
";
print $sbjVal, "
";
print $msgVal, "
";

Read through the script. It should be self-explanatory. Try the script by clicking the Send button of the page at the browser.

You can modify the receive.pl script so that it will send the information to an email server. In the Internet, you have a web server and you have an email server. The information from the form is sent to the web server. From there, it can be sent by the modified script to an email box in an email server. I will write an independent series on how to send emails, soon.

Let us stop here for this part of the series. We continue in the next part.

Chrys.

Published by Chrys Forcha

I have more than 10 years experience in computer programming, software, electronics and telecommunications. I have a First Degree in Electronics and a Master's Degree in Technical Education. As well a...  View profile

To comment, please sign in to your Yahoo! account, or sign up for a new account.