Home Lesson 5: Reading input from forms-a 'form'idable challenge

To get information from the form to the script, it has to go through the server which the script runs on. There is a very easy way to store each of these into variables that the script can use. You do this by adding two lines into the top of your script:

require 'cgi-lib.pl'
&ReadParse(%in);

What these lines do are take the information that the was submitted on the form and stores them into a hash called %in. A hash is a new form of variable that we haven't learned yet. The way a hash works is it stores a value 'under' a key. It's like a table. Imagine the hash as this:

Keys: farm table window
Values: 34 good happy

To recall data from a hash, you would use something like this:

$data = $hash{'farm'}; $hello = $hash{'table'};

The hash is actually %hash, but when you want to put the information into a string, you have to use the $ in place of the %. This changes the information into string form. The {'farm'} and {'table'} is the name of the key within the hash. Now, $data equals '34' and $hello equals 'good'. Now, to use the information that was submitted, and is now stored in %in, you just have to store it to a string so that it is easy to use. To do that, you should use something like this:

$myname = $in{'myname'};

The keys are the same as the names of the fields on the form. That may be confusing, so here is an example of a simple cgi form that asks for your name:

This is the page and the form:

Name:

It would look like this:

<HTML><BODY>
<FORM ACTION="cgi-bin/myname.cgi">
Name: <INPUT TYPE=text NAME="name">
<INPUT TYPE=submit>
</FORM>
</BODY></HTML>

The script should start out like this:

#!bin/user/local/perl

require 'cgi-lib.pl';
&ReadParse(%in);

$myname=$in{'myname'};

This is only the beginning of the script. There are many things that you can do with someone's name, or anything else that people submit. But that is covered in another lesson.

Chapter 4: Forms-ya mean I have to fill out another one? Home Chapter 6: Reading and writing to files-submitted with style


<<< Chapter 4 | Home | Chapter 6 >>>