Click to See Complete Forum and Search --> : Writing PERL, need advanced help
hazmtrl
06-17-2003, 02:58 PM
Ok, so I am writing a cgi so when a user fills out a form and they do not fill in a required field it errors out tell the user that the required field needs something there. This cgi sends an email with the information also saves the information in a directory for future reference. So far, it's coming along pretty good, the problem I am having is that the form comes back saying there is nothing in the required fields though I filled the form out completely and correctly. I'm sure it is something simple and I am over looking but my eyes are not catching it. I wrote this script on Thursday, but everyday since then I have spent hours trying to figure out where my error is. Has anyone ran into this problem before or can help me out. I am pretty familiar with PERL, but no genious at it. Thanks in advance for any help.
EscapeCharacter
06-18-2003, 01:51 AM
field verification on forms it usually done on the client side with javascript, though i may have misunderstood you.
nowonmai
06-18-2003, 05:48 AM
what CGI method are you using? GET or POST?
can you post the code (or at least the relevant bit)
hazmtrl
06-18-2003, 09:30 AM
I'm a beginner but this is what I have so far.
#!/usr/bin/perl
# Parse from Subroutine
sub parse_form; {
local ($name,$value);
# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
# Split the name-value pairs
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
# Un-Webify
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
# Remove NULL characters
value =~ s/\0//g;
value =~s/<!--(.|\n)*-->//g;
}
$FORM{$name} = $value;
}
# Get variables
sub get_variables {
if ($FORM{companyname}) {
$company = "$FORM{companyname}";
}
else {
&error(no_companyname);
}
if ($FORM{firstname}) {
$first = "$FORM{firstname}";
}
else {
&error(no_first);
}
if ($FORM{lastname}) {
$last = "$FORM{lastname}";
}
else {
&error(no_last);
}
if ($FORM{emailaddress} =~ /.*\@.*\..*/) {
$email = "$FORM{emailaddress}";
}
else {
&error(no_email);
}
if ($FORM{emailverify} =~ /.*\@.*\..*/) {
$everify = "$FORM{emailverify}";
}
else {
&error(no_everify);
}
if ($FORM{phonenumber}) {
$phone = "$FORM{phonenumber}";
}
else {
&error(no_phone);
}
if ($FORM{faxnumber}) {
$fax = "$FORM{faxnumber}";
}
if ($FORM{addressline1}) {
$address = "$FORM{addressline1}";
}
else {
&error(no_address);
}
if ($FORM{addressline2}) {
$address2 = "$FORM{addressline2}";
}
if ($FORM{city}) {
$city = "$FORM{city}";
}
else {
&error(no_city);
}
if ($FORM{state}) {
$state = "$FORM{state}";
}
else {
&error(no_state);
}
if ($FORM{countryother}) {
$country = "$FORM{countryother}";
}
if ($FORM{zipcode}) {
$zip = "$FORM{zipcode}";
}
else {
&error(no_zip);
}
if ($FORM{payableto}) {
$pay = "$FORM{payableto}";
}
else {
&error(no_pay);
}
if ($FORM{taxid}) {
$tax = "$FORM{taxid}";
}
else {
&error(no_tax);
}
if ($FORM{sitename}) {
$site = "$FORM{sitename}";
}
else {
&error(no_site);
}
if ($FORM{siteurl} =~ /.*\:.*\..*/) {
$url = "$FORM{siteurl}";
}
else {
&error(no_url);
}
if ($FORM{addlurl1} =~ /.*\:.*\..*/) {
$url1 = "$FORM{addlurl1}";
}
if ($FORM{addlurl2} =~ /.*\:.*\..*/) {
$url2 = "$FORM{addlurl2}";
}
if ($FORM{addlurl3} =~ /.*\:.*\..*/) {
$url3 = "$FORM{addlurl3}";
}
if ($FORM{agreeanswer} =~ Yes) {
$agree = "$FORM{agreeanswer}";
}
else {
&error(no_agree);
}
}
# Send Mail
open (MESSAGE,"|/usr/lib/sendmail -t"); {
print MESSAGE "To: me\@site.com\n";
print MESSAGE "FROM: $FORM{emailaddress}\n";
print MESSAGE "Subject: Web Form Entry\n\n";
print MESSAGE "Company Name: $FORM{'companyname'}\n";
print MESSAGE "First Name: $FORM{'firstname'}\n";
print MESSAGE "Last Name: $FORM{lastname}\n";
print MESSAGE "Email Address: $FORM{emailaddress}\n";
print MESSAGE "Verify Email Address: $FORM{emailverify}\n";
print MESSAGE "Phone Number: $FORM{phonenumber}\n";
print MESSAGE "Fax Number: $FORM{faxnumber}\n";
print MESSAGE "Address 1: $FORM{addressline1}\n";
print MESSAGE "Address 2: $FORM{addressline2}\n";
print MESSAGE "City: $FORM{city}\n";
print MESSAGE "State: $FORM{state}\n";
print MESSAGE "Foreign: $FORM{countryother}\n";
print MESSAGE "Zip: $FORM{zipcode}\n";
print MESSAGE "Check Payable To: $FORM{payableto}\n";
print MESSAGE "Tax Payer ID Type: $FORM{taxidtype}\n";
print MESSAGE "Tax Payer ID: $FORM{taxid}\n";
print MESSAGE "Site Name: $FORM{sitename}\n";
print MESSAGE "Site URL: $FORM{siteurl}\n";
print MESSAGE "Additional URL:\n";
print MESSAGE "$FORM{addurl1}\n";
print MESSAGE "$FORM{addurl2}\n";
print MESSAGE "$FORM{addurl3}\n";
print MESSAGE "Agreement: $FORM{agreeanswer} to the terms and conditions of the Agreement.\n";
close(MESSAGE);
&thank_you;
}
# Confirmation Page
# FOR TESTING ONLY, REPLACE WHEN RUNNING ###########
sub thank_you {
print "Content-type: text/html\n\n";
print "<html>\n";
print "<head>\n";
print "<meta http-equiv=\"refresh\" content=\"1; url=http://www.sitename.com/affil_confirm.html\">\n";
print "</head>\n";
print "<body bgcolor=ffffff>\n";
print "</body>\n";
print "</html>\n";
exit(0);
}
# REPLACE SITE WITH VIRTUAL PAGE WHEN RUNNING #######
sub error {
$error = $_[0];
print "Content-type: text/html\n\n";
if ($error eq 'no_companyname') {
print "<html><head><title>ERROR</title></head>\n";
print "<body bgcolor=ffffff text=990000><center><font size=-1>** Required fields left blank **<br>Company Name<br></fo
&rest_of_form;
}
elsif ($error eq 'no_first') {
print "<html><head><title>ERROR</title></head>\n";
print "<body bgcolor=ffffff text=990000><center><font size=-1>** Required fields left blank **<br>First Name<br></font
&rest_of_form;
}
elsif ($error eq 'no_last') {
print "<html><head><title>ERROR</title></head>\n";
print "<body bgcolor=ffffff text=990000><center><font size=-1>** Required fields left blank **<br>Last Name<br></font>
&rest_of_form;
}
elsif ($error eq 'no_email') {
print "<html><head><title>ERROR</title></head>\n";
print "<body bgcolor=ffffff text=990000><center><font size=-1>** Required fields left blank **<br>Email Address<br></f
&rest_of_form;
}
elsif ($error eq 'everify') {
print "<html><head><title>ERROR</title></head>\n";
print "<body bgcolor=ffffff text=990000><center><font size=-1>** Required fields left blank **<br>Email Verify<br></fo
&rest_of_form;
}
elsif ($error eq 'no_phone') {
print "<html><head><title>ERROR</title></head>\n";
print "<body bgcolor=ffffff text=990000><center><font size=-1>** Required fields left blank **<br>Phone Number<br></fo
&rest_of_form;
}
elsif ($error eq 'no_address') {
print "<html><head><title>ERROR</title></head>\n";
print "<body bgcolor=ffffff text=990000><center><font size=-1>** Required fields left blank **<br>Address<br></font>Pl
&rest_of_form;
}
elsif ($error eq 'no_city') {
print "<html><head><title>ERROR</title></head>\n";
print "<body bgcolor=ffffff text=990000><center><font size=-1>** Required fields left blank **<br>City<br></font>Pleas
&rest_of_form;
}
elsif ($error eq 'no_state') {
print "<html><head><title>ERROR</title></head>\n";
print "<body bgcolor=ffffff text=990000><center><font size=-1>** Required fields left blank **<br>State<br></font>Plea
&rest_of_form;
}
elsif ($error eq 'no_zip') {
print "<html><head><title>ERROR</title></head>\n";
print "<body bgcolor=ffffff text=990000><center><font size=-1>** Required fields left blank **<br>Zip<br></font>Please
&rest_of_form;
}
elsif ($error eq 'no_pay') {
print "<html><head><title>ERROR</title></head>\n";
print "<body bgcolor=ffffff text=990000><center><font size=-1>** Required fields left blank **<br>Payment<br></font>Pl
&rest_of_form;
}
elsif ($error eq 'no_tax') {
print "<html><head><title>ERROR</title></head>\n";
print "<body bgcolor=ffffff text=990000><center><font size=-1>** Required fields left blank **<br>Tax ID<br></font>Ple
&rest_of_form;
}
elsif ($error eq 'no_site') {
print "<html><head><title>ERROR</title></head>\n";
print "<body bgcolor=ffffff text=990000><center><font size=-1>** Required fields left blank **<br>No Website<br></font
&rest_of_form;
}
elsif ($error eq 'no_url') {
print "<html><head><title>ERROR</title></head>\n";
print "<body bgcolor=ffffff text=990000><center><font size=-1>** Required fields left blank **<br>No Website URL<br></
&rest_of_form;
}
elsif ($error eq 'no_agree') {
print "<html><head><title>ERROR</title></head>\n";
print "<body bgcolor=ffffff text=990000><center><font size=-1>** Required fields left blank **<br>Agreement<br></font>
&rest_of_form;
}
else {
print "<html><head><title>ERROR</title></head>\n";
print "<body bgcolor=ffffff text=990000><center><font size=-1>** Undefined Error **<br></font>Please Go back and Resub
&rest_of_form;
}
}
sub rest_of_form {
open (CONF, './conf.html');
while (<CONF>) {
print $_;
}
close (HEADER)
}
dchidelf
06-18-2003, 08:38 PM
Could you post the html for the form that posts to this cgi script?
hazmtrl
06-18-2003, 09:03 PM
** Couple of Notes ** This html was written by an outside source roughly a year ago. I have just been hired to make the verification work. Nothing in a hurry, more for the experience. There could have been a Drag and drop html program, but not for certain. I have also edited states, canadian provinces, also the large text agreement section because of space.
<form method="post" action="apply2.cgi">
<div align="center">
<table border="0" cellspacing="0" cellpadding="0" width="680">
<tr>
<td>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td>
<h2 class="heading"><font face="arial, helvetica, verdana, sans-serif">site.com Affiliate Membership Application</font></h2>
<p><font face="arial, helvetica, sans-serif" size="2">The site.com Affiliate Program provides an excellent opportunity for you to earn additional income. To join, you must provide the information requested. First, read the Affiliate Program Membership Agreement below, or read the <a href="site.html">Agreement on a printer friendly page</a>. Then complete the following application form. When you are finished, click the "Join Now!" button. You will receive an email verification with information on your account and instructions on how to set up your Affiliate Links to our discount airfare booking engine.
</font></p>
<p><font face="arial, helvetica, sans-serif" size="2">Required items are described in</font> <font face="arial, helvetica, sans-serif" size="2" color="990000"><b>red text</b></font> <font face="arial, helvetica, sans-serif" size="2">and starred</font> <font face="arial, helvetica, sans-serif" size="2" color="990000"><b>**</b></font><font face="arial, helvetica, sans-serif" size="2">.</font></p>
</td>
</tr>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td class="formopt" width="35%"> </td>
<td width="45%"> </td>
<td width="30%"> </td>
</tr>
<tr>
<td class="formopt" width="35%"> </td>
<td class="formopt" width="45%">Company and Contact Information </td>
<td width="30%"> </td>
</tr>
<tr>
<td class="formreq" width="35%" align="right">** Company Name </td>
<td width="45%"><input type="text" name="01-CompanyName" size="30" maxlength="75" value=""></td>
<td width="30%"> </td>
</tr>
<tr>
<td class="formreq" width="35%" align="right">** Contact First Name </td>
<td width="45%"><input type="text" name="02-FirstName" size="30" maxlength="50" value=""></td>
<td width="30%"> </td>
</tr>
<tr>
<td class="formreq" width="35%" align="right">** Contact Last Name </td>
<td width="45%"><input type="text" name="03-LastName" size="30" maxlength="50" value=""></td>
<td width="30%"> </td>
</tr>
<tr>
<td class="formreq" width="35%" align="right">** Affiliate Email Address </td>
<td width="45%"><input type="text" name="04-EmailAddress" size="30" maxlength="50" value=""></td>
<td width="30%"> </td>
</tr>
<tr>
<td class="formreq" width="35%" align="right">** Verify Email </td>
<td width="45%"><input type="text" name="05-EmailVerify" size="30" maxlength="50" value=""></td>
<td width="30%"> </td>
</tr>
<tr>
<td class="formreq" width="35%" align="right">** Phone Number </td>
<td width="45%"><input type="text" name="06-PhoneNumber" size="30" maxlength="20" value=""></td>
<td width="30%"> </td>
</tr>
<tr>
<td class="formopt" width="35%" align="right"> Fax Number </td>
<td width="45%"><input type="text" name="07-FaxNumber" size="30" maxlength="20" value=""></td>
<td width="30%"> </td>
</tr>
<tr>
<td class="formreq" width="35%" align="right">** Address Line 1 </td>
<td width="45%"><input type="text" name="08-Address Line1" size="30" maxlength="75" value=""></td>
<td width="30%"> </td>
</tr>
<tr>
<td class="formopt" width="35%" align="right"> Address Line 2 </td>
<td width="45%"><input type="text" name="09-Address Line2" size="30" maxlength="75" value=""></td>
<td width="30%"> </td>
</tr>
<tr>
<td class="formreq" width="35%" align="right">** City </td>
<td width="45%"><input type="text" name="10-City" size="30" maxlength="75" value=""></td>
<td width="30%"> </td>
</tr>
<tr>
<td class="formreq" width="35%" align="right">** State/Province </td>
<td width="45%" colspan="2">
<select name="11-state">
<option value="-1">Select...</option>
<option value="-1">-----US States-----</option>
<option value="-1">-----Canadian Provinces-----</option>
</select>
<span class="formsmall">US and Canada</span>
</td>
<td width="30%"> </td>
</tr>
<tr>
<td class="formopt" width="35%" align="right">Foreign Country </td>
<td width="45%" colspan="2"><input type="text" name="12-CountryOther" maxlength="20" size="30" value=""><span class="formsmall">Enter if other than US or Canada</span>
</td>
<td width="30%"> </td>
</tr>
<tr>
<td class="formreq" width="35%" align="right">** Zip/Postal Code </td>
<td width="45%"><input type="text" name="13-Zip Code" size="30" maxlength="75" value=""></td>
<td width="30%"> </td>
</tr>
<tr>
<td class="formopt"> </td>
<td> </td>
<td width="30%"> </td>
</tr>
<tr>
<td class="formopt"> </td>
<td class="formopt">Account Information</td>
<td width="30%"> </td>
</tr>
<tr>
<td class="formreq" width="35%" align="right">** Make Checks Payable To </td>
<td width="45%"><input type="text" name="20-PayableTo" size="30" maxlength="40" value="">
</td>
<td width="30%"> </td>
</tr>
<tr>
<td valign="top" class="formreq" width="35%" align="right">** Tax Payer ID <select name="TaxIDType" size="1"><OPTION VALUE='TaxID'>Tax ID<option value='Social Security Number'>SSN</SELECT>
</td>
<td width="45%"><input type="text" name="21-TaxID" size="20" maxlength="30" value="">
<span class="formsmall">Enter - U.S. Only</span></td>
<td width="30%"> </td>
</tr>
<tr>
<td class="formopt"> </td>
<td> </td>
<td width="30%"> </td>
</tr>
<tr>
<td class="formopt"> </td>
<td class="formopt">Your Website Information</td>
<td width="30%"> </td>
</tr>
<tr>
<td class="formreq" width="35%" align="right">** Your Site Name </td>
<td width="45%"><input type="text" name="25-SiteName" size="30" maxlength="150" value=""></td>
<td width="30%"> </td>
</tr>
<tr>
<td class="formreq" width="35%" align="right">** Your Site URL </td>
<td width="45%"><input type="text" name="26-SiteURL" size="30" maxlength="150" value="http://"></td>
<td width="30%"> </td>
</tr>
<tr>
<td class="formopt" width="35%" align="right">Additional URLs that will link <br />
to Airline Consolidator.com
</td>
<td width="45%">
<input type="text" name="27-AddlURL1" size="30" maxlength="150" value="http://"><br />
<input type="text" name="28-AddlURL2" size="30" maxlength="150" value="http://"><br />
<input type="text" name="29-AddlURL3" size="30" maxlength="150" value="http://">
</td>
<td width="30%"> </td>
</tr>
<tr>
<td class="formopt" width="35%"> </td>
<td width="45%"> </td>
<td width="30%"> </td>
</tr>
<tr>
<td class="formopt" width="35%"> </td>
<td class="formopt" width="45%">Affiliate Agreement </td>
<td width="30%"> </td>
</tr>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="center">
<font face="arial, helvetica, sans-serif" size="2">
<textarea cols="75" name="AgreementText" rows="15" readonly>
Extremely long agreement text box
</textarea>
</td>
</tr>
<tr>
<td align="center" class="formtext"><p>
You can also read the Agreement on a <a href="site.html">printer friendly page</a>...</p>
</td>
</tr>
</table>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td class="formtext"><p><span class="heading"> <br />To sign up now...</span><br />
Read our Affiliate Agreement. To join, select "I agree" below and then click on the "Join Now" button.
</p>
<tr>
<td class="formreq"><p><br />** Select one:
</td>
</tr>
<tr>
<td class="formtext">
<select name="30-AgreeAnswer" size="1">
<option value="Yes">I agree
<option value="No" selected>
I do not agree
</select>
to the terms and conditions of the Affiliate Program Agreement.
</td>
</tr>
<tr>
<td><br /><input type="Submit" value="Join Now!"></td>
</tr>
</table>
</form>
dchidelf
06-18-2003, 10:15 PM
In the HTML form the input fields are named like:
01-CompanyName
so you would reference them with
$FORM{01-CompanyName} in your cgi script.
-or rename them in the HTML-
hazmtrl
06-19-2003, 01:34 AM
someone should come slap the stupid out of me. hahaha I changed all of those, but did not save the changes. And I have been racking my brain trying to figure out what I did wrong. Figured it was something that I was over looking. Thanks a lot for looking through that for me.
arn0ld
06-19-2003, 08:24 PM
for future work, have you considered cgi.pm?
hazmtrl
06-20-2003, 01:30 AM
Not familiar with cgi.pm, can you explain a little?
arn0ld
06-20-2003, 08:32 AM
also for future work, use more google, e.g.:
perl cgi.pm
Top search results include:
http://stein.cshl.org/WWW/software/CGI/
http://www.wiley.com/legacy/compbooks/stein/