Current location: Hot Scripts Forums » General Community » The Lounge » comment form - hi guys, i just can't figure this out!


comment form - hi guys, i just can't figure this out!

Reply
  #1 (permalink)  
Old 11-16-03, 08:39 PM
spider0116 spider0116 is offline
New Member
 
Join Date: Nov 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Question comment form - hi guys, i just can't figure this out!

i've been trying to figure this out for the longest time...


i built a comment form page a site. i had the "contact.html" page with
perl behind it. after i uploaded both files, i know the script was not
working correctly because no email is actually shooting through.
i've checked everything and make sure the site host support perl and
everything i can possibly think of. at this point i am wondering
if anything can help me look over my code. thanks! -spt


________________________________________perl starts here
#!/usr/bin/perl
print "Content-type: text/html\n\n";

$title='Feedback Form';
$to='information@aptsandlofts.com';
$from= 'information@aptsandlofts.com';
$formurl = "http://www.aptsandlofts.com/contact.html" ;
$subject='Feedback Form';
$errorurl = "http://www.aptsandlofts.com/error.html" ;
$thankyouurl = "http://www.aptsandlofts.com/thankyou.html" ;



open(MAIL, "|/usr/sbin/sendmail -t");

close(MAIL);




# ---------- functions -----------
sub redirect_url {
my ( $url ) = shift ;
print "Location: $url\n\n" ;
}

sub parse_form_data {
my ($request_method, $input_string, $content_length) ;
my (@vars, $indiv_var, $name, $value);
my (%form) ;

$request_method = $ENV{'REQUEST_METHOD'} ;
$content_length = $ENV{'CONTENT_LENGTH'} ;

# load the entire string into $input_string
if ($request_method =~ /post/i) {
$input_string = "" ;
read( STDIN, $input_string, $content_length ) ;
}
else {
$input_string = $ENV{ 'QUERY_STRING' };
}
unless (defined $input_string) {
$input_string = "" ;
}

# put all the variable pairs (name=value) into an array
@vars = split( /&/, $input_string );

# process each individual name, value pair, putting them
# into a hash for easy access
foreach $indiv_var ( @vars ) {

# separate the pair
($name, $value) = split( /=/, $indiv_var, 2 );

# translate encoding
$name =~ s/%([\da-fA-F]{2})/pack("C", hex($1))/eg;
$name =~ tr/+/ /;
unless (defined $value) {
# just in case there was no equals as in ISINDEX
$value = "" ;
}
$value =~ s/%([\da-fA-F]{2})/pack("C", hex($1))/eg;
$value =~ tr/+/ /;

# put the pair in the hash for easy access
$form{$name} = $value ;
}

return wantarray ? %form : undef ;
}

sub send_email {
my ($mailprog, $email, $name, $mailto, $subject, $message) = @_ ;

if (open MAIL, "|$mailprog -t") {
print MAIL "To: $mailto\n" ;
print MAIL "From: \"$name\" <$email>\n" ;
print MAIL "Reply-To: \"$name\" <$email>\n" ;
print MAIL "X-Mailer: chfeedback.pl 2.01\n" ;
print MAIL "Subject: $subject\n\n" ;
print MAIL $message ;
close MAIL ;
}
# ignore fails, just don't do anything

return ;
}

# ----------- main program ------

my %form = parse_form_data();

my $email ;
if (exists $form{"email"}) {
$email = $form{"email"} ;
}
else {
redirect_url( $formurl );
exit ;
}
if ($email eq "") {
redirect_url( $errorurl );
exit ;
}

my $name = $form{"name"} ;
if ($name eq "") {
redirect_url( $errorurl );
exit ;
}

my $comments = $form{'comments'} ;
if ($comments eq "") {
redirect_url( $errorurl );
exit ;
}

my $http_referer = $ENV{ 'HTTP_REFERER' };
my $message =
"This message was sent from:\n" .
"$http_referer\n" .
"------------------------- COMMENTS -------------------------\n\n" .
$comments .
"\n\n------------------------------------------------------------\n" ;

send_email ( $mailprog, $email, $name, $mailto, $subject, $message );
redirect_url( $thankyouurl );
Reply With Quote
  #2 (permalink)  
Old 11-17-03, 07:27 PM
Chas Chas is offline
Coding Addict
 
Join Date: Oct 2003
Location: California
Posts: 359
Thanks: 0
Thanked 0 Times in 0 Posts
I looked it over quickly and spotted a couple of problems.

For starters:
Code:
#!/usr/bin/perl
use strict;
use warnings;
Try to get into the practice of using strict and warnings. If nothing else, at least while you are developing the application. The addition of those two lines alone would have tipped you off to a lot of the problems.

Next, use CGI.pm to parse form data, it will eliminate the need for your parse_form_data sub:

Code:
use CGI;
my $in = new CGI;

# It is also great for printing headers
print $in->header();
Then you can access your form data like so:

Code:
my $email = $in->param('email');

# instead of:
my $email ;
if (exists $form{"email"}) {
$email = $form{"email"} ;
}
As far as not sending the mail, here is the problem:

Code:
send_email ( $mailprog, $email, $name, $mailto, $subject, $message );

# You need something like this:
my $mailprog = '/usr/sbin/sendmail';
send_email ( $mailprog, $email, $name, $mailto, $subject, $message );
You never define $mailprog in your script. Using strict would have let you know about that. You should also be checking to make sure that you did in fact open sendmail:

Code:
open MAIL, "|$mailprog -t" or die "Cannot open sendmail.  Reason: $!";
That script would have died and left a nice message for you in the error_log.

One last thing, try to post perl specific questions in the perl forum. You'll get a better response there. Maybe a mod can move it over for you.

That should be enough to get you going for now. If you have more problems, post back and I can take a closer look at it when I'm not in as much of a hurry. Include any errors from your error log if you have access to it. Oh, this might help too: http://learn.perl.org/library/beginning_perl/. Nothing beats a free book.

~Charlie
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
lock form fields using checkbox jonathen JavaScript 5 02-04-05 03:35 PM
question on form and submit TxRanger JavaScript 2 11-03-03 11:28 AM
SQL database registration form help vinhkhuong PHP 3 10-10-03 03:49 AM
asp: URGENT! need to change code to create new form per id seala ASP 2 09-09-03 09:54 PM
asp: checkboxes & multi-page form seala ASP 0 09-02-03 01:58 PM


All times are GMT -5. The time now is 03:10 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.