PDA

View Full Version : " if " statement not working


Script_Junkie
11-14-2005, 12:26 PM
I tested to make sure the variables where set correctly, they are. I don't see whats wrong, all my other " if " statments in the rest of the script work. Even ones that use the exact same query.

Working sections are in green, not working sections are red

$hull_cleaning=yes , $leave_zincs=yes , $prop_polish=yes, $vessel_location=(can be marina, another_location, or home)



if(($page eq '5')||(($page eq '4')&&($vessel_location eq 'home'))) {
print <<End;
<input type='hidden' name='page' value=6>
<input type='hidden' name='title' value='Thank you'>
<br>
<table>
<tr>
<td width=250>
<font color=$font_color size=3>$first_name &nbsp; $last_name</font>
<br>
<font color=$font_color size=3>$address_street</font>
<br>
<font color=$font_color size=3>$address_city , &nbsp; $address_state &nbsp; $address_zip</font>
<p>
<font color=$font_color size=3>Phone: $phone</font>
<br>
<font color=$font_color size=3>Fax: $fax</font>
<br>
<font color=$font_color size=3><a href='mailto: $email '> $email </a></font>
<br>
<font color=$font_color size=3>Contact By: $contact_by</font>
<br>
End
if($contact_by eq 'phone') {
print "<font color=$font_color size=3>Between: &nbsp;", $first_call_time, "&nbsp; and &nbsp;", $second_call_time, "&nbsp;", $am_pm, "</font>";
}
print <<End;
</td>
<p>
<td width=350>
<font color=$font_color size=4>Vessel Information:</font>
<br>
<font color=$font_color size=3> $vessel_name , $fl_number</font>
<br>
<font color=$font_color size=3>$vessel_size Ft. $manufacturer , $model</font>
<p>
<font color=$font_color size=4>Services Required:</font>
<br>
End
if($hull_cleaning eq 'yes') {
print "<font color=$font_color size=3>Clean hull every &nbsp;", $service_interval, "&nbsp;</font>";
}
if($leave_zincs eq 'yes') {
print "<font color=$font_color size=3>(leave zincs on dock)</font><br>";
} else { ###<-- else statment here seems to work even if $leave_zincs=yes
print "<font color=$font_color size=3>(do not leave zincs on dock)</font><br>";
}
if($prop_polish eq 'yes') {
print "<font color=$font_color size=3>Polish prop every &nbsp;", $polish_interval, "</font><br>";
}
print "<p><font color=$font_color size=4>Special instructions and/or Comments:</font>";
print "<br><font color=$font_color size=3>", $comments, "</font><br><p>";
if($vessel_location eq 'home') {
print "</td></tr></table>";
&finish_page;
exit (0);
}
print "</td></tr><tr><td><font color=$font_color size=4>Vessel Location:</font><br>";
if($vessel_location eq 'marina') {
print "<font color=$font_color size=3>Marina Name: &nbsp;", $marina_name, "</font><br>";
}
if($vessel_location eq 'another_location') {
print "<font color=$font_color size=3>Location Name: &nbsp;", $location_name, "</font><br>";
}
print <<End;
<font color=$font_color size=3> $vessel_address_street </font>
<br>
<font color=$font_color size=3> $vessel_address_city , $vessel_address_state , $vessel_address_zip </font>
<br>
<font color=$font_color size=3> Dock: $dock &nbsp; Slip: $slip </font>
</td>
</tr>
</table>
End
&finish_page;
}

FishMonger
11-14-2005, 03:33 PM
How are the variables being assigned? How did you make sure they were set correctly? Have you verified that the variables don't have an embedded "\n"?

Use the Data::Dumper module and print out each variable.

use Data::Dumper;
print Dumper ($contact_by, $hull_cleaning, $leave_zincs, $prop_polish, $vessel_location);

Script_Junkie
11-14-2005, 11:21 PM
Right before the spots that were not working i added them like so:


print <<End;
</td>
<p>
<td width=350>
<font color=$font_color size=4>Vessel Information:</font>
<br>
<font color=$font_color size=3> $vessel_name , $fl_number</font>
<br>
<font color=$font_color size=3>$vessel_size Ft. $manufacturer , $model</font>
<p>
<font color=$font_color size=4>Services Required:</font>
<br>
$hull_cleaning , $service_interval , $leave_zincs , $prop_polish , $polish_interval , $vessel_location
End
if($hull_cleaning eq 'yes') {
print "<font color=$font_color size=3>Clean hull every &nbsp;", $service_interval, "&nbsp;</font>";
}
if($leave_zincs eq 'yes') {
print "<font color=$font_color size=3>(leave zincs on dock)</font><br>";
} else {
print "<font color=$font_color size=3>(do not leave zincs on dock)</font><br>";
}
the result i got was : yes,3weeks,yes,yes,home(because i selected home on the form)

Maybe i should not have edited that part out when submiting it.

None the less, I will try your suggestion. However in other parts of my script when i query "if($vessel_location = 'marina')" or "if($vessel_location = 'another_location')" the if statment works.

I would post the whole script, but there's not one comment anywhere in it, and you would all despise me! i never intended anyone to read it but me, it only has one purpose and isn't really intended to be publicly used.

Script_Junkie
11-14-2005, 11:43 PM
It seems i can't use Data::Dumper, it doesn't like the fact i use a variable for my font color.


"#ffffff" is not exported by the Data::Dumper module
Can't continue after import errors at sign_up.cgi line 16
BEGIN failed--compilation aborted at sign_up.cgi line 16.


13:use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
14:use Data::Dumper
15:
16:$font_color = '#ffffff'; #was useing "white", hex didn't work either
17:$font_color2 = '#00e2e2';
18:$title = param('title');

FishMonger
11-15-2005, 12:11 AM
The error regarding the Dumper module is due to the fact that you forgot to terminate the line with a semi colon.

Script_Junkie
11-15-2005, 12:15 AM
Doh! i'll try it again

Script_Junkie
11-15-2005, 12:21 AM
print dumper ($contact_by, $hull_cleaning, $leave_zincs, $prop_polish, $vessel_location)

dumper results: yes , 3weeks , yes , yes , 6months , marina

Is it just me, or is there a space after each one. Could that be the problem?

FishMonger
11-15-2005, 12:21 AM
"if($vessel_location = 'marina')" or "if($vessel_location = 'another_location')"

The reason those are woking is because there assignments not comparison tests.

if($vessel_location eq 'marina')" or "if($vessel_location eq 'another_location')

Script_Junkie
11-15-2005, 12:27 AM
sry that was an example (it's how i think), i always use "eq" in any if statment, unless it was compairing 2 #'s if you look back at my posts you'll see i used "eq".

FishMonger
11-15-2005, 12:45 AM
It would be helpful if you were a little more precise on the code you're showing. Please post the code and results that you actually used/received.
print dumper ($contact_by, $hull_cleaning, $leave_zincs, $prop_polish, $vessel_location)

dumper results: yes , 3weeks , yes , yes , 6months , marina
That line of code won't produce the type of result that you posted.

Here's a more accurate example.
#!/usr/bin/perl -w
use Data::Dumper;

($hull_cleaning='yes', $leave_zincs='yes', $prop_polish='yes', $vessel_location='marina');
print Dumper ($hull_cleaning, $leave_zincs, $prop_polish, $vessel_location);

Outputs

$VAR1 = 'yes';
$VAR2 = 'yes';
$VAR3 = 'yes';
$VAR4 = 'marina';
Without having proper representation of what your doing and your results, I will have a much harder time troubleshooting your problem

Script_Junkie
11-15-2005, 02:34 AM
First off it would indeed help if i pasted the right code into the discussion, sorry.

The content of the variables changes depending on how i fill out the preceding form, however in order to get my desired effect i need to fill out the form in such a way that all the "if" statments result in "true" so i fill it out so that:

$contact_by = phone
$hull_cleaning = yes
$leave_zincs = yes
$prop_polish = yes
$vessel_location = marina OR another_location

so the following statments will be true and run the elclosed code


if($contact_by eq 'phone') {
print "<font color=$font_color size=3>Between: &nbsp;", $first_call_time, "&nbsp; and &nbsp;", $second_call_time, "&nbsp;", $am_pm, "</font>";
}

if($hull_cleaning eq 'yes ') {
print "<font color=$font_color size=3>Clean hull every &nbsp;", $service_interval, "&nbsp;</font>";
}

if($leave_zincs eq 'yes ') {
print "<font color=$font_color size=3>(leave zincs on dock)</font><br>";
} else {
print "<font color=$font_color size=3>(do not leave zincs on dock)</font><br>";
}

if($prop_polish eq 'yes ') {
print "<font color=$font_color size=3>Polish prop every &nbsp;", $polish_interval, "</font><br>";
}

if($vessel_location eq 'marina ') {
print "<font color=$font_color size=3>Marina Name: &nbsp;", $marina_name, "</font><br>";
}

if($vessel_location eq 'another_location ') {
print "<font color=$font_color size=3>Location Name: &nbsp;", $location_name, "</font><br>";
}


then i used the following to double check the values of the above mentioned variables:


use Data::Dumper;
print Dumper ($contact_by, $hull_cleaning, $leave_zincs, $prop_polish, $vessel_location);


it returned "phone , yes , yes , yes , marina

that being what is required to run the statment above, i don't see whats wrong

HOWEVER my cable modem has been receiveing 100,000 errors a min, and now i am having problems i wasn't haveing before (script unchanged) so maybe thats the whole problem

Script_Junkie
11-15-2005, 02:55 AM
OMG! I"M AN IDIOT i figured it out! it was the spaces.

each time the page fliped to the next the hidden input tag was adding a space to the begining and the end of the variable value
example:

<input type='hidden' name='first_name' value=' $first_name '>

so on page 2 $first_name would be " luke " (space at begining and end)
and on page 3 $first_name would be " luke " (2 spaces at begining and end)
page 4 $first_name would be " luke " (3 spaces)
and so on

so by the time you get to page 4 or 5 $hull_cleaning's value was " yes " not "yes"

FishMonger
11-15-2005, 03:27 AM
Yep, after seeing that last code segment (about an hour ago) that's what i thought, but I wanted to see if you would catch that when you printed them out with the Dumper module. Good Work!