In this sixteenth slide, we introduce a mail form which, like the form
validator, either displays the mail form or execute the CGI program
processing it, based on a simple test:
if (%qx) { process the form } else { display the form }
1. THE MAIL FORM:
The program consists of three parts:
In Part I, we use the CGI library to evaluate the variable pairs and check
if the form was submitted:
#!/usr/local/bin/perl
MAIN:
{
use CGI qw(:standard);
$query=new CGI;
print $query->header;
print "<html><head><title>Mail Form</title></head>\n";
print "<body>";
print "<h1>Mail Form<h1>\n";
foreach $ii (sort $query->param())
{
$jj=$query->param($ii);
$qx{$ii}=$jj;
#print "${ii}: ${jj}<br>";
}
In Part II, we assume that the form was submitted:
if (%qx)
{
$first=$qx{'first'};
$last=$qx{'last'};
$email=$qx{'email'};
$message=$qx{'message'};
print "<b>You entered the following information:<b><br>";
print "First: ${first}<br>";
print "Last: ${last}<br>";
print "email: ${email}<br>";
print "message: ${message}<br>";
&MailX($first,$last,$email,$message);
print "<b>Your message was mailed back to you.<b><br>";
}
and in Part III, we assume that the form is displayed for the first time:
else
{
print "<form>\n";
print "First Name: <input type=\"text\" name=\"first\">";
print "<br>Last Name: <input type=\"text\" name=\"last\">";
print "<br>email address: <input type=\"text\" name=\"email\">";
print "<br><textarea name=\"message\"><textarea>";
print "<br><input type=\"submit\">";
print "</form>";
}
print "</body></html>";
}
finally, in part 4, we provode the mail function as a subroutine:
sub MailX
{
local($first,$last,$email,$msg)=@_;
$dear="Dear ${first} ${last}:";
open(MAIL,"|mailx -r jclevin\@nova.edu ${email}");
print MAIL $dear;
print MAIL $msg;
close (MAIL);
}
2. FORM DESCRIPTION:
In Part I, the most important section is a foreach statement where the
variable pairs are read into an associative array: %qx:
foreach $ii (sort $query->param())
{
$jj=$query->param($ii);
$qx{$ii}=$jj;
#print "${ii}: ${jj}<br>";
}
If the form was submitted, the associative array is not empty, we go to
part II, we collect the information posted on the form (first, last name, email
address, etc), then call the MailX subroutine:
&MailX($first,$last,$email,$message);
If not, we go to part III, and display the form:
print "<form>\n";
print "First Name: <input type=\"text\" name=\"first\">";
print "<br>Last Name: <input type=\"text\" name=\"last\">";
print "<br>email address: <input type=\"text\" name=\"email\">";
print "<br><textarea name=\"message\"><textarea>";
print "<br><input type=\"submit\">";
print "</form>";
Finally, we open the mail and send our message:
open(MAIL,"|mailx -r jclevin\@nova.edu ${email}");
print MAIL $dear;
print MAIL $msg;
close (MAIL);
3. PROGRAM EXECUTION:
Access the program by clicking on the following link:
Mail Form
or, using your browser, type the following URL:
http://www.claywall.com/cgi-bin/CGI/mail.cgi
|