Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Please help with a Perl Script

Status
Not open for further replies.

Byager

Programmer
Oct 14, 1998
10
US
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.<br>
<br>
<br>
#!/usr/bin/perl<br>
<br>
require '<A HREF=" TARGET="_new"><br>
$posted=&getformvars(*FORM);<br>
<br>
$mailprog='/usr/sbin/sendmail';<br>
<br>
$instructions_url=&quot;<A HREF=" TARGET="_new"><br>
if(!($FORM{'mailto'})) {<br>
&bounce($instructions_url);<br>
exit 0;<br>
}<br>
<br>
#HERE<br>
<br>
if($FORM{'required'}) {<br>
$required=$FORM{'required'};<br>
$required=~ s/\[//g;<br>
$required=~ s/\]//g;<br>
@req=split(/ /, $required);<br>
$counter=0;<br>
while($counter &lt;= $#req) {<br>
if(!($FORM{$req[$counter]})) {<br>
&failure;<br>
}<br>
$counter++;<br>
}<br>
}<br>
<br>
open(MAIL, &quot;¦ $mailprog &quot;.$FORM{'mailto'}) ¦¦ die &quot;Can't open $mailprog!\n&quot;;<br>
print MAIL &quot;Reply-to: &quot;.$FORM{'mailto'}.&quot;\n&quot;;<br>
print MAIL &quot;Errors-to: &quot;.$FORM{'mailto'}.&quot;\n&quot;;<br>
print MAIL &quot;From: &quot;.$FORM{'mailto'}.&quot;\n&quot;;<br>
print MAIL &quot;To: &quot;.$FORM{'mailto'}.&quot;\n&quot;;<br>
if($FORM{'subject'}) {<br>
print MAIL &quot;Subject: &quot;.$FORM{'subject'}.&quot;\n&quot;;<br>
} else {<br>
print MAIL &quot;Subject: E-Mail Form Results\n&quot;;<br>
}<br>
print MAIL &quot;========================================\n\n&quot;;<br>
<br>
$counter=1;<br>
while($FORM{'m'.$counter}) {<br>
$theline=$FORM{'m'.$counter};<br>
$theline=~ s/\[([A-Za-z0-9]*)\]/$FORM{$1}/g;<br>
print MAIL $theline.&quot;\n&quot;;<br>
$counter++;<br>
}<br>
<br>
print MAIL &quot;\n========================================\n&quot;;<br>
print MAIL &quot;Entry made from &quot;.$ENV{'REMOTE_ADDR'}.&quot;\n&quot;;<br>
print MAIL &quot;Referring URL: &quot;.$ENV{'HTTP_REFERER'}.&quot;\n&quot;;<br>
<br>
close(MAIL);<br>
<br>
&success;<br>
<br>
exit 0;<br>
<br>
sub success {<br>
if($FORM{'success'}) {<br>
&bounce($FORM{'success'});<br>
} else {<br>
print &quot;Content-type: text/html\n\n&quot;;<br>
print &quot;&lt;html&gt;\n&lt;body&gt;\n&lt;center&gt;&lt;font size=5&gt;Thanks!&lt;/font&gt;&lt;/center&gt;\n&lt;/body&gt;\n&lt;/html&gt;\n&quot;;<br>
}<br>
return;<br>
}<br>
<br>
sub failure {<br>
if($FORM{'failure'}) {<br>
&bounce($FORM{'failure'});<br>
} else {<br>
print &quot;Content-type: text/html\n\n&quot;;<br>
print &quot;&lt;html&gt;\n&lt;body&gt;\n&lt;center&gt;&lt;font size=5&gt;You didn't enter the required information&lt;/font&gt;&lt;p&gt;&lt;font size=4&gt;Please &lt;a href=\&quot;&quot;.$ENV{'HTTP_REFERER'}.&quot;\&quot;&gt;return&lt;/a&gt; to the form.&lt;/center&gt;\n&lt;/body&gt;\n&lt;/html&gt;\n&quot;;<br>
}<br>
exit 0;<br>
}<br>
<br>
sub bounce {<br>
local ($url)=@_;<br>
print &quot;Content-type: text/html\n&quot;;<br>
print &quot;Location: &quot;.$url.&quot;\n\n&quot;;<br>
print &quot;&lt;html&gt;&lt;body&gt;&lt;/body&gt;&lt;/html&gt;\n&quot;;<br>
return;<br>
}<br>
<br>
<br>
Thanks to all,<br>
<br>
Brian Yager
 
I'm not completely sure, but suspect that if you comment out:<br>
<br>
<br>
<br>
if(!($FORM{'mailto'})) {<br>
&bounce($instructions_url);<br>
exit 0;<br>
}<br>
<br>
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.<br>
<br>
-- Heidi
 
Here again, I'm not completely certain but I'd look in the library file you require.<br>
<br>
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 :)<br>
<br>
It appears to me that 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'.<br>
<br>
I've added comments to the code below.<br>
<br>
# if the value referenced by $FORM{'required'} is not null<br>
# enter this block of code<br>
# so the quick and dirty solution is to comment out this <br>
# block of code but I think you can customize things<br>
# so read on.<br>
<br>
if($FORM{'required'}) {<br>
<br>
# now store the value referenced by 'required' <br>
# in the variable $required<br>
# I assume this value will be a string containing the<br>
# names of all the required fields<br>
# e.g. $required='[field1] [field2] [field3]'<br>
<br>
$required=$FORM{'required'};<br>
<br>
# replace any [ chars with null<br>
$required=~ s/\[//g;<br>
<br>
# replace any ] chars with null<br>
$required=~ s/\]//g;<br>
<br>
# now $required should look like this<br>
# 'field1 field2 field3'<br>
# split $required on the spaces and fill the values into<br>
# array @req<br>
@req=split(/ /, $required);<br>
<br>
$counter=0;<br>
<br>
# while $counter is &lt;= the subscript of the last <br>
# element in the array, loop.<br>
# if there are 3 elements in @req (0,1,2), $#req == 2<br>
while($counter &lt;= $#req) {<br>
<br>
# here again we're using a string to reference a value<br>
# in the %FORM hash, if the value is null, meaning the <br>
# field in the form was not completed, we call the <br>
# failure subroutine<br>
# so, if the first element in our array is 'field1'<br>
# the first time through $FORM{$req[$counter]} ==<br>
# $FORM{$req[0]} == $FORM{'field1'} <br>
if(!($FORM{$req[$counter]})) {<br>
&failure;<br>
}<br>
$counter++;<br>
}<br>
}<br>
<br>
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.<br>
<br>
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.<br>
<br>
e.g.<br>
<br>
$debug=
 
Interesting, the posting software picked up on my $debug="TRUE" and truncated the response.<br>
<br>
foreach $key (keys(%FORM))<br>
{<br>
print "key $key = " . $FORM{$key} . "\n" ;<br>
}<br>
<br>
Hope that helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top