A Form Example

Overview
Objectives
Installing Apache
Installing Perl
Lesson 1
Hello World
Anatomy of a CGI Program
Hello World with CGI.pm
Lesson 2
HTML Forms and DOM
POST and GET
A Form Example
Lesson 3
Cookie Tutorial
Database Tutorial

Software Links
FAQ / Terminology
Contact the Author

 

Consider the following HTML:

      <HTML>
        <HEAD>
          <TITLE>HTML Form Example</TITLE>
          <LINK rel="stylesheet" href="../css/perlcgi.css" type="text/css">
        </HEAD>
        <BODY>
          <FORM method="GET" action="/cgi-bin/perl_tut/lesson2.pl">
            <h3>Your First Name:</h3>
            <p><INPUT type="text" name="FirstName"></p>
            <h3>Your Last Name:</h3>
            <p><INPUT type="text" name="LastName"></p>
            <h3>Check if you are a student:</h3>
            <p><INPUT type="checkbox" name="isStudent"></p>
            <h3>What is your gender?</h3>
            <p>
              <INPUT type="radio" name="Gender" value="isMale">Male
              <INPUT type="radio" name="Gender" value="isFemale">Female
            </p>
            <h3>How many moons are there on your planet?</h3>
            <p>
            <SELECT name="numMoons" size="5">
              <OPTION value="1" selected>Only One Moon
              <OPTION value="2">Two Nice Moons
              <OPTION value="3">A Fine Triplet
              <OPTION value="4">Four Celestial Bodies
              <OPTION value="5-8">Between Five and Eight
              <OPTION value="9-12">We Have Between Nine and Twelve
              <OPTION value="lots">Too Many To Count!
            </SELECT>
            </p>
            <h3>Comments:</h3>
            <p><TEXTAREA rows="10" cols="80">Type Comments Here</TEXTAREA></p>
            <h3>Hidden Data!</h3>
            <p><INPUT type="hidden" name="Secret" value="Invisible"></p>
            <h3>Submit this Form</h3>
            <p><INPUT type="submit" value="Send Data Now!"></p>
            <h3>Reset this Form</h3>
            <p><INPUT type="reset" value="Clear all my input now"></p>
        </FORM>
      </BODY>
    </HTML>

Which is available from the following URL

http://inconnu.isu.edu/~ink/perl_cgi/lesson2/form_example.htm
This form passes it's information via the GET method to the following Perl CGI program:

      #!/usr/bin/perl	 
      use strict;
      use CGI;
      my $cgi = new CGI;
      print
        $cgi->header() .
        $cgi->start_html( -title => 'Form Results',
                          -author => 'Craig Kelley',
                          -style => '/~ink/perl_cgi/css/perlcgi.css') .
        $cgi->h1('Form Results') . "\n";
      my @params = $cgi->param();
      print '<TABLE border="1" cellspacing="0" cellpadding="0">' . "\n";
      foreach my $parameter (sort @params) {
        print "<tr><th>$parameter</th><td>" . $cgi->param($parameter) . "</td></tr>\n";
      }
      print "</TABLE>\n";
      print $cgi->end_html . "\n";
      exit (0);
	  

And produces the following output, with my sample information entered:

http://inconnu.isu.edu/cgi-bin/perl_tut/lesson2.pl

Play around with the code, and see how each form tag is encoded in the CGI URL. Fiddling with this code should give you all the knowledge you need in order to build basic CGI applications with Perl. Everything after this involves topics such as session managment (cookies, or otherwise) and database topics. Be certain that you understand what's going on here, and that you have a similar working example running on your machine before proceeding.


All pages written by Craig Kelley unless otherwise specified. Please use the Contact link from the menu to submit changes or suggestions. Permission is given to use this tutorial in any way you wish including re-publishing or "mirroring". The most up-to-date version of this document currently resides at http://inconnu.isu.edu/~ink/perl_cgi.

This page updated: May 6, 2002 22:21