CGI Program with Form

Consider the following HTML:

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title>HTML Form Example</title>
  5. <link rel="stylesheet" href="../css/perlcgi.css" type="text/css" />
  6. </head>
  7. <body>
  8. <form method="get" action="/cgi-bin/perl_tut/lesson2.pl">
  9.   <h3>Your First Name:</h3>
  10.   <p>
  11.     <input type="text" name="FirstName" />
  12.   </p>
  13.   <h3>Your Last Name:</h3>
  14.   <p>
  15.     <input type="text" name="LastName" />
  16.   </p>
  17.   <h3>Check if you are a student:</h3>
  18.   <p>
  19.     <input type="checkbox" name="isStudent" />
  20.   </p>
  21.   <h3>What is your gender?</h3>
  22.   <p>
  23.     <input type="radio" name="Gender" value="isMale" />
  24.     Male
  25.     <input type="radio" name="Gender" value="isFemale" />
  26.     Female </p>
  27.   <h3>How many moons are there on your planet?</h3>
  28.   <p>
  29.     <select name="numMoons" size="5">
  30.       <option value="1" selected="selected">Only One Moon </option>
  31.       <option value="2">Two Nice Moons </option>
  32.       <option value="3">A Fine Triplet </option>
  33.       <option value="4">Four Celestial Bodies </option>
  34.       <option value="5-8">Between Five and Eight </option>
  35.       <option value="9-12">We Have Between Nine and Twelve </option>
  36.       <option value="lots">Too Many To Count! </option>
  37.     </select>
  38.   </p>
  39.   <h3>Comments:</h3>
  40.   <p>
  41.     <textarea rows="10" cols="80">Type Comments Here</textarea>
  42.   </p>
  43.   <h3>Hidden Data!</h3>
  44.   <p>
  45.     <input type="hidden" name="Secret" value="Invisible" />
  46.   </p>
  47.   <h3>Submit this Form</h3>
  48.   <p>
  49.     <input type="submit" value="Send Data Now!" />
  50.   </p>
  51.   <h3>Reset this Form</h3>
  52.   <p>
  53.     <input type="reset" value="Clear all my input now" />
  54.   </p>
  55. </form>
  56. </body>
  57. </html>

This form passes it's information via the GET method to the following Perl CGI program:


  1. #!/usr/bin/perl  
  2.  
  3. use strict;
  4. use CGI;
  5. my $cgi = new CGI;
  6. print
  7.   $cgi->header() .
  8.   $cgi->start_html( -title => 'Form Results') .
  9.   $cgi->h1('Form Results') . "\n";
  10. my @params = $cgi->param();
  11. print '<table border="1" cellspacing="0" cellpadding="0">' . "\n";
  12. foreach my $parameter (sort @params) {
  13.   print "<tr><th>$parameter</th><td>" . $cgi->param($parameter) . "</td></tr>\n";
  14. }
  15. print "</table>\n";
  16. print $cgi->end_html . "\n";
  17. exit (0);

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

Perl Form CGI

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.