|
|
Question : Error Trapping
|
|
Below is what my perl looks like, what i would like to do is if there is no DATA whatsoever in the param then redirect to the contact form page or print the error.
"I am no cgi perl expert and am learning it. This may not be the best approach, but i am a noobe at perl"
i tried if(!$cgi->param($key)){ print "No Vars"; }
But it did not work i still got a blank email
######################################## #!/usr/bin/perl use CGI::Carp qw(fatalsToBrowser); use CGI; $cgi = new CGI;
for $key ( $cgi->param() ) { $input{$key} = $cgi->param($key); }
print "Content-Type: text/html\n\n";
################## # SEND MAIL PATH # ################## $mailprog = '/usr/lib/sendmail -i -t';
$sbj = "SSC Contact Form"; $comment = $input{comments};
$to = "me\@mySite.net";
$email = "$input{email}"; $name = "$input{fname} $input{lname}";
# Open The Mail Program open(MAIL,"|$mailprog");
print MAIL "To: $to\n"; print MAIL "From: $email ($name)\n"; print MAIL "Subject: $sbj\n\n";
print MAIL "Name: $name\n"; print MAIL "Address: $input{address}\n"; print MAIL "City: $input{city}\n"; print MAIL "State: $input{state}\n"; print MAIL "Zip: $input{zip}\n"; print MAIL "Phone: $input{phone}\n"; print MAIL "Email: $email\n\n"; print MAIL "Comment: $comment\n\n";
close (MAIL);
######################### # I print the HTML success page here # #########################
TIA jAy
|
Answer : Error Trapping
|
|
This: foreach ($cgi->param() ) { if( ! $cgi->param($_) )
only tests whether the parameters passed to your script actually have values. In other words, a url such as this: /cgi-bin/script.pl?vara= will cause the redirect, but a url such as this: /cgi-bin/script.pl will not cause the redirect, because the body for the foreach loop is never executed.
To see if there are parameters at all, do: my @r = $cgi->param(); if( scalar @r <= 0 ) { ### we have parameters
|
|
|
|
|