Mike Gossland's Perl Tutorial Course for Windows


Intro to GET | CGI Module | Handling Forms | Form Errors | Fatal Errors


Chapter 6. The CGI Module

Handling Form Input

To see how the CGI module helps you handle form input, let's look at another simple example.

Here is just a plain form with a text area and a select list. The form's action could be set to POST.

(Note that the Add Me and Don't Add Me buttons are not functional here.)

Name:

Here is code that could present that form:


<form method="POST" action="/cgi-bin/example1.pl" name="frmAnimal">
	<input type="hidden" name="origin" value="course">
	Name: <input type="text" name="name" value="" size="24">
	<select name="animal">
		<option value="Baboon">Baboon
		<option value="Chimpanzee">Chimpanzee
		<option value="Orangutan">Orangutan
		<option value="Gorilla">Gorilla</select>
	<p></p>
	<input type="Submit" name="request" value="Add Me!">
	<input type="Submit" name="request" value="Don't Add Me!">
</form>


Here's the script that gets could be called as the action of the form:


#!/usr/bin/perl
use CGI;

$cgi = new CGI;
for $key ( $cgi->param() ) {
	$input{$key} = $cgi->param($key);
}
print qq(Content-type: text/html

<html>
<head>
</head>
<body>
    You are $input{name}
    You wish to be a $input{animal}
    You pushed the $input{request} button
</body></html>);


It creates a web page on the fly for you.

Pretty straightforward now that you know how to edit files on the fly. Once you get the data from the user, you can do anything you like with it; you can use it to customize content, or stash it in a database somewhere.