|
2.
Using the CGI Perl Module
OK,
so you can now say "Hello" to the world. You are a programmer
at last. Now we want to introduce some interactivity!
We
want to ask a question on a web page and give an appropriate response.
We do this by passing parameters.
The
easiest way to pass parameters to a cgi program is with a form.
(form tutorial) When you create
a form, each field on the form has a name and an associated value,
which is whatever you type into the field.
<FORM METHOD="POST" ACTION="/tutorial-scripts/form1.cgi">
What is your favourite colour: <INPUT NAME="colour">
<INPUT TYPE="submit">
</FORM>
which
looks like this:
View
CGI Program
When
you click on the submit button, the browser passes those name=value
pairs to the cgi program specified in the form in an encoded format.
For example, if you entered "red", the form would return
colour=red
The
CGI.pm module
The CGI Perl module (CGI.pm) provides a simple way to create Perl
CGI programs. You can use the CGI module to take care of the HTTP
protocol requirements, and to extract the parameters of these name=value
pairs.
To
include the CGI module in your program just add the following line
near the top:
use
CGI qw(:cgi);
To
extract a value from a form, use the param() function. From the
previous example param("colour") returns the value "red"
Have
a good look at the CGI code and you will know what is going on.
Passing
a Parameter to CGI.pm
This
is a simple program passing one parameter to the CGI.pm module.
In the form shown below, the text field is called "myname".
The
cgi program calls the CGI.pm module to extract the "myname"
parameter from the form and load it into a variable called $who.
It then generates an HTML page which prints the contents of this
variable.
View
HTML of above form
View
CGI Program
Things
to note
To
use the CGI Perl module, insert the line use CGI qw(:cgi); at the
top.
Use the param() function to extract the value of the textfield.
Passing
Multiple Parameters to CGI.pm
This is a more advanced program as it passes multiple parameters
to the CGI.pm module.
In the form shown below, the text fields are called "name",
"phone" and "food".
The fields "name" and "phone" contain just one
value each, but "food" can contain many values. The cgi
program assumes all parameters could contain multiple values. It
loops through all the parameters putting their contents in an array
called "@values" and prints a nicely HTML formatted line
of the name=value pairs, separating the values with commas.
View
HTML of above form
View
CGI Program
Things
to note
When using Multi-Choice fields, you must place the values into an
array.
So
we are getting a little bit more complex. Analyse the code and you
will see what is happening. Stick with it and do not worry too much
about multi-choice fields as this is only an optional extra.
Ok,
ready for the next step...
Back - Next
We
hope this tutorial was helpful. If you like, you can link to our
tutorials from your web site. Just use the following code:
<a
href="http://www.ezi-hosting.com/tutorials.htm">Free
Scripting Tutorials from EziHosting</a>
|