Article

Programming Perl 101

Page: 1 2 3 4 5 6 7 8 Next

4.) Display.cgi

Now that we've got the form up (call the file whatever you like, just make sure that it has a .html or .htm extension) we'll move onto creating the Perl script to work with the data. The way this entire thing will work is:

1. A user will view a form and fill out the data

2. Each field on the form and the data entered will have a name slapped to it

3. The data entered, together with the name of the form field, will be sent to display.cgi for processing

Let's create display.cgi!

First, the shebang line:

#!/usr/local/bin/perl

Next, insert this:

require "subparseform.lib";
&Parse_Form;

Subparseform.lib is a widely used file that will aid us in the creation of this script. Do a search for it on the web and you should be able to find it. If your search fails, contact me and I'll give you some help: mailto:chris@mycoding.com .

Now, remember: we want this script to take the information from the form and display it to the user in such a way as to say "This is the information you entered." To do this we need to tell the script to "grab" the form information. But how? We simply have to match up those tags!

Enter this next:

$first = $formdata{'first};
$city = $formdata{'city'};
$food = $formdata{'food'};

Look familiar?

Take notice of a few things: first, the ";" sign is at the end of every line, as it should be. Second, the tag names: They match exactly with the tags on the form information.

Take note of the equals sign ("="). That tells the script to assign the data entered into the form to a variable or container which we can use inside the Perl script. The first line is saying "make $first equal the information entered in the form field with the name 'first'".

As you've probably noticed, first, city and food have a dollar sign in front of them. Perl requires that all variables or containers used in a script begin with the $ sign.

That first line, as I said before, is telling the browser that whatever form field had the name of "first" in the HTML file, now has the name of "$first".

What this allows us to do is use "$first" variable elsewhere in the Perl. What will happen is that $first will be replaced by whatever the user entered into the "first" field on the feedback form!

Starting to see where this is going?

If you liked this article, share the love:
Print-Friendly Version Suggest an Article

Sponsored Links

Follow SitePoint on...