×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Please help with a Perl Script

Please help with a Perl Script

Please help with a Perl Script

(OP)
I am NOT a Perl programmer. I have a script that I need to modify and I am pretty clueless. Below I will list the script and maybe someone can help me understand it better. I am a programmer so I understand some of it. The script is an email form script. My client wants me to make the fields NOT be required. Any and all help is appreciated.


#!/usr/bin/perl

require 'www.pl';

$posted=&getformvars(*FORM);

$mailprog='/usr/sbin/sendmail';

$instructions_url="http://www.traveller.com/tech/start/emailform/";;

if(!($FORM{'mailto'})) {
&bounce($instructions_url);
exit 0;
}

#HERE

if($FORM{'required'}) {
$required=$FORM{'required'};
$required=~ s/\[//g;
$required=~ s/\]//g;
@req=split(/ /, $required);
$counter=0;
while($counter <= $#req) {
if(!($FORM{$req[$counter]})) {
&failure;
}
$counter++;
}
}

open(MAIL, "¦ $mailprog ".$FORM{'mailto'}) ¦¦ die "Can't open $mailprog!\n";
print MAIL "Reply-to: ".$FORM{'mailto'}."\n";
print MAIL "Errors-to: ".$FORM{'mailto'}."\n";
print MAIL "From: ".$FORM{'mailto'}."\n";
print MAIL "To: ".$FORM{'mailto'}."\n";
if($FORM{'subject'}) {
print MAIL "Subject: ".$FORM{'subject'}."\n";
} else {
print MAIL "Subject: E-Mail Form Results\n";
}
print MAIL "========================================\n\n";

$counter=1;
while($FORM{'m'.$counter}) {
$theline=$FORM{'m'.$counter};
$theline=~ s/\[([A-Za-z0-9]*)\]/$FORM{$1}/g;
print MAIL $theline."\n";
$counter++;
}

print MAIL "\n========================================\n";
print MAIL "Entry made from ".$ENV{'REMOTE_ADDR'}."\n";
print MAIL "Referring URL: ".$ENV{'HTTP_REFERER'}."\n";

close(MAIL);

&success;

exit 0;

sub success {
if($FORM{'success'}) {
&bounce($FORM{'success'});
} else {
print "Content-type: text/html\n\n";
print "<html>\n<body>\n<center><font size=5>Thanks!</font></center>\n</body>\n</html>\n";
}
return;
}

sub failure {
if($FORM{'failure'}) {
&bounce($FORM{'failure'});
} else {
print "Content-type: text/html\n\n";
print "<html>\n<body>\n<center><font size=5>You didn't enter the required information</font><p><font size=4>Please <a href=\"".$ENV{'HTTP_REFERER'}."\">return</a> to the form.</center>\n</body>\n</html>\n";
}
exit 0;
}

sub bounce {
local ($url)=@_;
print "Content-type: text/html\n";
print "Location: ".$url."\n\n";
print "<html><body></body></html>\n";
return;
}


Thanks to all,

Brian Yager

RE: Please help with a Perl Script

I'm not completely sure, but suspect that if you comment out:



if(!($FORM{'mailto'})) {
&bounce($instructions_url);
exit 0;
}

then it wouldn't kick the user to the instruction page. You can comment lines out just by adding # in front of them. Just be sure to comment out each of these lines. I think that will remove the requirements to enter something into the mailto field.

-- Heidi

RE: Please help with a Perl Script

Here again, I'm not completely certain but I'd look in the www.pl library file you require.

As Heidi suggests, you can comment out the check for the mailto field. That would eliminate that one source of error but then again a script that sends mail without a mailto address would be pretty useless :)

It appears to me that www.pl creates a hash called %FORM. If you don't know what a Perl hash is, think of it as a 2 dimensional array with the first dimension being a key string and the second being the value referenced by the key string. So $FORM{'required'} retrieves the value in the hash referenced by the key string 'required'.

I've added comments to the code below.

# if the value referenced by $FORM{'required'} is not null
# enter this block of code
# so the quick and dirty solution is to comment out this
# block of code but I think you can customize things
# so read on.

if($FORM{'required'}) {

# now store the value referenced by 'required'
# in the variable $required
# I assume this value will be a string containing the
# names of all the required fields
# e.g. $required='[field1] [field2] [field3]'

$required=$FORM{'required'};

# replace any [ chars with null
$required=~ s/\[//g;

# replace any ] chars with null
$required=~ s/\]//g;

# now $required should look like this
# 'field1 field2 field3'
# split $required on the spaces and fill the values into
# array @req
@req=split(/ /, $required);

$counter=0;

# while $counter is <= the subscript of the last
# element in the array, loop.
# if there are 3 elements in @req (0,1,2), $#req == 2
while($counter <= $#req) {

# here again we're using a string to reference a value
# in the %FORM hash, if the value is null, meaning the
# field in the form was not completed, we call the
# failure subroutine
# so, if the first element in our array is 'field1'
# the first time through $FORM{$req[$counter]} ==
# $FORM{$req[0]} == $FORM{'field1'}
if(!($FORM{$req[$counter]})) {
&failure;
}
$counter++;
}
}

So as I say, the key is the definition of the hash value $FORM{'required'} this should be a string containing all the required fields.

Remember, print is your buddy. Print out the values in the hash and the array. Once you figure things out, just comment the prints back out. If you get a lot of prints, add a $debug variable that turns them on and off and comment/uncomment it at the top of the script.

e.g.

$debug=

RE: Please help with a Perl Script

Interesting, the posting software picked up on my $debug="TRUE" and truncated the response.

foreach $key (keys(%FORM))
{
print "key $key = " . $FORM{$key} . "\n" ;
}

Hope that helps.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close