Tags

Perl Forms

One example of such a form is the workshop registration site, which uses perl code stored at /nfs/www/httpd/perl/dhcpreqtest. A second example is the laptop registration site, which uses the perl file at /nfs/web/restricted/rbin/outreach/verify.

If this is your first time using perl and html to create a form, it may be best to follow along in the attached perl and html files.

Part I: Modifying an Existing html Template

The html page will call the perl form that you create. For example, here is the start of a form that is based on perl code called verify, and located in /rbin/outreach.
<form action="http://www.lepp.cornell.edu/restricted/rbin/outreach/verify" method="post">
<input type="hidden" name="page" value="WorkshopRegistration">

The next part of the html code is:
<input type="hidden" name="Subject" value="Workshop Registration (PFPT)">
<input type="reset" value="Erase fields and start over"><br>
You can change the Subject value from "Workshop Registration (PFPT)" and you can use this Subject for the email message that the perl code will send (you don't have to do so, however). The second line adds a reset button that displays the text Erase_button.jpg

For the first question, we can start a numbered list and we will ask for the person's name. The perl form will access the answer by using the value assigned to input name, which is "Name". Since value ="" there is no initial value in this field.
  <ol>
    <li><label for="name"><b>What is your name?</b></label>
      <input name="Name" size="60" value="" id="name"><br>
      <p>
      </p>
    </li>
This code results in: name_question.jpg

For every subsequent question, follow the same format from li to /li, which indicates the start of a new line item within a list. There are different types of response types you can choose other than the text box described above. One form is radio buttons:
<li><label for="lodging"><b>What is your lodging preference?</b></label>
      <br>
      <input name="Lodging" type="radio" value="OnCampus"> I would like on-campus lodging for $55 per night.
      <br>
      <input name="Lodging" type="radio" value="Other"> I will make other arrangements.
      <br>
      <input name="Lodging" type="radio" value="None"> I will not be staying over night.
      <p>
      </p>
</li>
This code results in: radio1.jpg

The perl code can access the answer by using "Lodging", and the values assigned to Lodging are either "OnCampus", "Other", or "None". After the > sign are the options that are displayed as text.

If you want to have one of the radio buttons initially selected, add "checked" before the > symbol. For example:
<li><b>Would you like to use this laptop during CLEO Meetings in Clark?</b></label>
      <input name="Clark" type="radio" value="Yes"> Yes
      <input name="Clark" type="radio" value="" checked> No
</li>
This code results in: radio2.jpg

For more types of input boxes (such as checkboxes, menus, etc.) search for a site that explains html forms, such as this one.

You can also change the text that appears in the Submit button. The form ends with the following code:
    <label for="mailit"><input type="submit" value="   Send   " id="mailit">
      <input type="hidden" name="EndPage" value="--eom--">
    </li>
  </ol>
</form>
You can change Submit's value to be different from "Send" if you like. The result of this code is: submit.jpg

Part II: Modifying Existing perl Code

This example follows the attached perl code, which sends an email to one person. It also does not create a review page after the user has entered all the data and before it is sent to the administrator. The documentation for an implementation with a confirmation page can be found here.

Near the top of the file, the recipient of the emailed data is listed as my $to and the variable my @fields is an array of values that we can store the data entered in specific form questions.
my $to = "chris237\@gmail.com";

my $query = new CGI;

my @fields = ( 'Name', 'Email', 'Phone', 'InstName', 'InstStreet', 'InstCity', 'InstState', 
'InstZip', 'Lodging', 'ContactName', 'ContactPhone' );
In Part I, the questions in the html file had input name = "Name", "Email", etc. The perl code can access the data entered into those fields, and we will use the code to email all of this information to someone ($to).

Now, toward the end of the file, we see the section responsible for the email properties.
    my $msg = Mail::Internet->new(Body => \@body);
    my $head = $msg->head;
    
    $head->replace('Subject', $query->param('Subject'));
    $head->replace('From', $query->param('Email'));
    $head->replace('To', $to);
You can change the "Subject, From, To" fields if you desire.

Below that, there is the message that prints the data the user entered. Above the data it prints a message ("Please review..."), then it prints the data (print @body) and then it prints another message ("Your registration...").
if ($msg->smtpsend(To => $to)) {
   # remove the remote host info
   pop @body;

   print qq(
       <p>Please review the following information and press the back button if changes are needed.</p>
       <pre>);
   print @body;
   print "</pre>
               <p>Your registration will be promptly reviewed. Thank you.</p>\n";
}

Sending a Confirmation Email

For this example, all of the above information is valid, but we will use the attached confirmation email example, which only differs from one person by an extra few lines of code.

Below the last part of code that was discussed above, we see the following. 'Clark' is a field in the form that we are checking to see if the user has entered a value, and it can be change to something in your form, such as 'Name'.
# Send confirmation email

if($query->param('Clark')){

A bit lower is the code for the second email.
        my $msg2 = Mail::Internet->new(Body => \@body2);   
        my $head2 = $msg2->head;   
        $head2->replace('Subject', $query->param('Subject'));
        $head2->replace('From', $query->param('Email'));
        $head2->replace('To', $query->param('Email'));
This sends a confirmation email to the address that the user entered ($query->param('Email')), instead of the address in the previous example that we sent manually ($to). Like above, you can change the subject of the email if you wish.

Adding a Confirmation Page Before Submission

The documentation for an implementation with a confirmation page can be found here.
Topic revision: r6 - 11 Aug 2008, DevinBougie
This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding CLASSE Wiki? Send feedback