PDA

View Full Version : CGI/Perl form question, please help!


still learning
07-07-2006, 07:01 PM
I have no knowledge with this perticular language. What im trying to do is, I have an online contact form that works great, except people can leave it blank and click submit. I want to make it where if someone leaves it blank a message like "Please fill out all required fields" will pop up.

also If possible I would like an email message to be sent to the user who fills out the form saying something like "We received your contact form, thanks" or whatever.. any help would be greatly appriciated. THANKS!!!

below is the code I have so far......................................


#!/usr/bin/perl -wT
#
# mail-form cgi
# Quick and dirty Web form processing script. Emails the data
# to the recipient.
#
# You should change any variable that has "CONFIGURE" in the
# comment.

#### modules (these don't need changing)
#
# be strict, define all variables
use strict;

# use CGI.pm for parsing form data and printing headers/footers
use CGI;

# redirect errors to the browser window
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);

# create an instance of CGI.pm:
my($cgi) = CGI->new;

# CONFIGURE: change this to the proper location of sendmail on your system:
my($mailprog) = '/usr/sbin/sendmail';

# Set the path, so taint mode won't complain. This should be the path
# that sendmail lives in! (If you're using /usr/lib/sendmail, then
# the path had better be /usr/lib here.)
$ENV{PATH} = "/usr/sbin";

# CONFIGURE: change this to the e-mail address you want to receive the
# mailed form data
my($recipient) = 'james@nospam.com';
my($cc) = 'james@nospam.com';
my($serveremail) = 'server@nospam.com';

# CONFIGURE: change this to your URL:
my($homepage) = "http://www.voicefactor.com/contactthank.htm";

# CONFIGURE: change this to the subject you want the e-mail to have
# WARNING: Do NOT Let the form specify the subject line, or your
# program can be hijacked by spammers!!!
my($subj) = "Online Contact Form Received";

# everything below this shouldn't need changing, except possibly the
# "thank you" page at the end.

# Print out a content-type header
print $cgi->header;

# CGI.pm automatically parses the data, and you can retrieve it using
# $cgi->param("fieldname")
my($sender) = $cgi->param("email");
my($csender) = $cgi->param("cemail");
if ($sender ne $csender) {
print "<html><head><title>Error!</title></head><body>\n<b><h3>Error: Email and Confirmation Email are not the same!</h3></b></body></html>\n\n";
exit;
}
# Now send mail to $recipient
open (MAIL, "|$mailprog -t") || &dienice("Can't open $mailprog!\n");
print MAIL "To: $recipient\n";
print MAIL "Cc: $cc\n";
print MAIL "From: $serveremail\n";
print MAIL "Subject: $subj\n\n";

# print all of the form fields:
foreach my $i ($cgi->param()) {
print MAIL $i . ": " . $cgi->param($i) . "\n";
}
print MAIL "\n\n";

# print some extra info:
print MAIL "Server protocol: $ENV{'SERVER_PROTOCOL'}\n";
print MAIL "HTTP From: $ENV{'HTTP_FROM'}\n";
print MAIL "Remote host: $ENV{'REMOTE_HOST'}\n";
print MAIL "Remote IP address: $ENV{'REMOTE_ADDR'}\n";
close (MAIL);

# Now send mail to $recipient
open (MAIL2, "|$mailprog -t") || &dienice("Can't open $mailprog!\n");
print MAIL2 "To: $sender\n";
print MAIL2 "From: $serveremail\n";
print MAIL2 "Subject: Online Contact Form Received\n\n";
print MAIL2 "\n";
print MAIL2 "We have received your online contact form and we will get back\n";
print MAIL2 "to you as soon as possible.\n";
print MAIL2 "\n\n";
print MAIL2 "VoiceFactor\n";
print MAIL2 "Customer Service Department\n";
close (MAIL2);

# CGI.pm prints the HTML header
print $cgi->start_html(-title=>"Thank You",
-bgcolor=>"#ffffff",
-text=>"#000000");

# CONFIGURE:
# Print a thank-you page - you can customize this however you wish.
# Be sure to use proper HTML, and escape any $-signs or @-signs with a
# backslash, e.g.: \$25.00, nullbox\@cgi101.com

print <<EndHTML;
<meta http-equiv="refresh" content="0;url=$homepage">
EndHTML

# CGI.pm prints the HTML footer
print $cgi->end_html;

# error handler
sub dienice {
my($msg) = @_;
print $cgi->start_html(-title=>"Error");
print qq(<h2>Error</h2>\n);
print $msg;
print $cgi->end_html;
exit;
}

# the end.

still learning
07-07-2006, 07:07 PM
This is the HTML side of the code...

------------------------------------------------------------


//--></script><!--webbot BOT="GeneratedScript" endspan -->
<form language="JavaScript" name="FrontPage_Form1" onsubmit="return FrontPage_Form1_Validator(this)" action="http://www.nospam.com/cgi-bin/contactform.cgi" method="post">
<p align="center"><strong><font size="2">What kind of comment are you sending?</font></strong></p>
<dl>
<dd>
<p align="center"><font size="2">
<input type="radio" value="Complaint" name="comment">Complaint
<input type="radio" value="Problem" name="comment">Problem
<input type="radio" checked="checked" value="Suggestion" name="comment">Suggestion
<input type="radio" value="Praise" name="comment">Praise</font> </p></dd></dl>
<p align="center"><strong><font size="2">What aspect of our business do you want to comment on?</font></strong></p>
<dl>
<dd>
<p align="center"><select size="1" name="subject">
<option value="" selected="selected">Web Site</option>
<option value="">Company</option>
<option value="">Products</option>
<option value="">Store</option>
<option value="">Employee</option>
<option value="">(Other)</option></select><font size="2"> Other:
<input type="text" maxlength="256" size="26" value="" name="other"></font> </p></dd></dl>
<p align="center"><strong><font size="2">Enter your comments in the space provided below:
<br></font></strong><font size="2"><textarea name="textbox" rows="11" cols="57">&nbsp;</textarea>
<br></font> </p>
<p align="center"><strong><font size="2">Tell us how to get in touch with you:</font></strong></p>
<p align="center"><strong style="font-weight: 400;"><font size="2">Please Note That All Fields Marked (<font color="#ff0000">*</font>) Are Required</font></strong></p>
<dl>
<dd>
<div align="center">
<table id="table4" width="46%" border="0">
<tbody>
<tr>
<td align="right" width="86">Name:<font color="#ff0000">*</font></td>
<td> <!--webbot bot="Validation" b-value-required="TRUE" i-minimum-length="2" -->
<input type="text" size="24" value="" name="name"></td></tr>
<tr>
<td align="right" width="86">Email:<font color="#ff0000">*</font></td>
<td> <!--webbot bot="Validation" b-value-required="TRUE" i-minimum-length="2" -->
<input type="text" size="24" value="" name="email"></td></tr>
<tr>
<td align="right" width="86">Confirm Email:<font color="#ff0000">*</font></td>
<td> <!--webbot bot="Validation" b-value-required="TRUE" i-minimum-length="2" -->
<input type="text" size="24" value="" name="cemail"></td></tr>
<tr>
<td align="right" width="86">Phone:</td>
<td>
<input type="text" size="24" value="" name="phone"></td></tr>
<tr>
<td align="right" width="86">Fax:</td>
<td>
<input type="text" size="24" value="" name="fax"></td></tr></tbody></table></div>
<p align="center"><font size="2">
<input type="checkbox" value="ContactRequested" name="ContactRequested"> Please contact me as soon as possible regarding this matter.</font>
</p></dd></dl>
<div align="center">
<input type="submit" value="Submit Comments">
<input type="reset" value="Clear Form">
<br><font color="#ff0000" size="4">NO SOLICITING</font></div>
<div align="center"><font color="#ff0000">&nbsp;</font></div>
<div align="center"><span style="font-size: 12pt; color: maroon; font-family: 'Arial Unicode MS';"><strong>*Please be sure to add nospam.com to your safe recipient list to ensure receipt of future emails.</strong></span></div></form>

----------------

Thank you again!

dswimboy
07-08-2006, 06:26 PM
i read an article long ago about form validation. while some of the material is dated [specifically the object model: use document.getElementById() instead], the concepts are all still the same. it appears your form already has a validator: FrontPage_Form1_Validator(this). i would use that to make your alert box that says "you haven't filled out all required fields"

the code you have should send an email to the form user saying "thanks for your form entry":
# Now send mail to $recipient
open (MAIL2, "|$mailprog -t") || &dienice("Can't open $mailprog!\n");
print MAIL2 "To: $sender\n";
print MAIL2 "From: $serveremail\n";
print MAIL2 "Subject: Online Contact Form Received\n\n";
print MAIL2 "\n";
print MAIL2 "We have received your online contact form and we will get back\n";
print MAIL2 "to you as soon as possible.\n";
print MAIL2 "\n\n";
print MAIL2 "VoiceFactor\n";
print MAIL2 "Customer Service Department\n";
close (MAIL2);

it looks like both things you wanted, are already in the script you have.