Perl Forms: Adding a Confirmation Page
The example we will use is the
Workshop Registration page. This html file calls a perl file named
verify, which itself calls the perl file
register, which acts as the confirmation page. Once the user has entered the data into the webpage and presses submit, the perl file
verify takes that data and fills in the confirmation form
register. This allows the user to change any entered data in
register before it sends out the emails.
If this is your first time using perl and html to create a form, it may be best to follow along in the perl files (verify and register).
Differences
The code in
register is similar to the code in the "one person" file in the
parent topic. For
verify, we replace a large part of the code, starting at line
my $msg = Mail::Internet->new(Body => \@body);
until the "trailer". In its place, we place the html form we created earlier. But this time, instead of referencing verify, it will call register. Start this with
print << "newpage" and end it with
newpage.
print << "newpage"
<form action="http://www.lepp.cornell.edu/restricted/rbin/outreach/register" method="post">
<input type="hidden" name="page" value="WorkshopRegistration">
[...]
newpage
An optional addition is to place a message above this form, telling the user to review the entered fields.
print qq(Please review the information you have entered. Press Submit to send your registration.);
Loading Data From verify Into register
In order to have the confirmation page appear as a form with data pre-loaded, we need to make a perl variable for each field. Here is an example for the field 'Name'.
my $submitted_name = $query->param('Name');
Now in the html code that we pasted into the perl file, we need to replace the initial value for field 'Name' with $submitted_name. Do this for each field.
<li><label for="name"><b>What is your name?</b></label>
<input name="Name" size="60" value="$submitted_name" id="name"><br>
<p>
</p>
</li>
A difficulty occurs when you want to set a radio button or a checkbox. This requires a bit of unwieldly code since we have to put the string "checked" by the user's choice. Here's what we did for a question about lodging with three possible choices:
my $subLodgeOnCampus = "";
my $subLodgeOther = "";
my $subLodgeNone = "";
if ($submitted_lodging eq "OnCampus"){
$subLodgeOnCampus = "checked";
}elsif ($submitted_lodging eq "Other"){
$subLodgeOther = "checked";
}elsif ($submitted_lodging eq "None"){
$subLodgeNone = "checked";
}
Then in the pasted html code, add the appropriate variable, which is either an empty string or "checked" before the > symbol. This way, at most one of the buttons will be checked.
<li><label for="lodging"><b>What is your lodging preference?</b></label>
<br>
<input name="Lodging" type="radio" value="OnCampus" $subLodgeOnCampus> I would like on-campus lodging.
<br>
<input name="Lodging" type="radio" value="Other" $subLodgeOther> I will make other arrangements.
<br>
<input name="Lodging" type="radio" value="None" $subLodgeNone> I will not be staying over night.
</li>
Now save the file and upload it to the web server where it is stored.
Setting the Confirmation Page Header and Trailer
For both register and verify, we will use perl functions to access the html code that prints the header and trailer. For this example, we store the functions in the attached file
HTML.pm.
The
HTML.pm file starts with the following chunk of perl code (which was taken from
here).
package HTML;
use strict;
BEGIN {
use Exporter ();
@HTML::ISA = qw(Exporter);
@HTML::EXPORT = qw();
@HTML::EXPORT_OK = qw($q);
}
use vars qw($q);
This will export the functions that we will call from register and verify.
The header function is written in the form
sub [name of function] { ... }. Before we add the html to the body of the function, we need the perl print statements to indicate the start and the end of html code. An example can be seen below:
sub printmyheader{
print <<"header";
[add html code here]
header
}
You can follow the same procedure to make a function for the trailer of the page.
Note: It is
conventional to end .pm and .pl files with a "1;". (A use statement evaluates the contents of a file and the last expression in the file must evaluate to true, or the eval dies.)
Having created the header and trailer functions in
HTML.pm, we want to call these functions in register and verify.
At the top of the file are the "use" statements (which are used to include perl
modules into your program). After the last "use" statement, insert the following code to allow the functions in our HTML.pm file to be accessed. (You will need to add
use CGI; if it is not already part of your program.)
use vars qw($q);
use lib qw(.);
use HTML qw($q); # HTML.pm is in the same dir as script.pl
$q = CGI->new;
Then, to call the header function that you created earlier, place it after the print function that is already in your program. An example calling
printmyheader() from the HTML.pm file is shown below.
print $query->header();
HTML::printmyheader();
The trailer function can be placed at the end of the file.
HTML::printmytrailer();