Page 1 of 1

Need some help with php code, cant seem to figure it out.

Posted: Thu May 23, 2002 3:55 pm
by pistolfire99
Hello Everyone,
I am new to php. I am currently learning php from a book called "Beginning PHP4" from wrox publications. I am copying the code exactly from the book and I am getting this weird error. I would appreciate if someone could please shed some light into this.
There are two pages, car.html and car.php. The first page 'car.html' has a form and sends some value to 'car.php' and does some calculations and displays the result.

car.html

Code: Select all

<html>
<body>

<div align="center">

<b>Car Rental Services</b><br><br>

<form method=POST action="car.php">

First Name:- <Input name="firstname" type="text"> <br><br>

Age:- <Input name="age" type="text"> <br><br>

Do you hold a valid California License:-
	<Input name="license" type="checkbox"> <br><br>

<Input type="Submit">
<Input type="Reset">


</form>

</div>

</body>
</html>
Below is the 'car.php' code

Code: Select all

<html>
<body>

<div align="center">

<b>Car Rental Company</b> <br><br>

<?php
	

	if ( $age > 20 && $license =="on" )
		
			echo ("You can rent the car<br><br>");
		
	if ( $age < 21 || $license =="" )
		
			echo ("Sorry, You are denied");
		

?>

<br><br>
<a href="car.html>click here</a>


</div>
</body>
</html>
And this is the error I am getting. This error only occurs when I use "age>21" and "license=off or unchecked"

Car Rental Company

Notice: Undefined variable: license in C:\Xitami\webpages\car.php on line 11

Notice: Undefined variable: license in C:\Xitami\webpages\car.php on line 15
Sorry, You are denied


I would appreciate if someone could shed some light into this.
I am using Xitami Web Server + PHP4.2 + MySQL 3.23 + WinXP Pro

I am at the point where I need to fix this. Until now, I have been ignoring such errors since I knew that the code was Ok.
my c:\windows\php.ini settings are "cgi.force_redirect = 0" & register_globals = On

Thank You,
Pistolfire99

Re: Need some help with php code, cant seem to figure it out

Posted: Thu May 23, 2002 4:17 pm
by duke9
pistolfire99 wrote:

Code: Select all

if ( $age > 20 && $license =="on" )
try

Code: Select all

if ( ($age > 20) && (isset($license) && ($license == "on")) )

Posted: Thu May 23, 2002 4:27 pm
by volka
and perhaps try phpinfo()

Posted: Thu May 23, 2002 4:46 pm
by pistolfire99
duke9:- Thanks for the tip, I dont get the errors, but now I cant display the correct answer when the condition is $license="" .. let me play around and I will figure it out.

volka:- phpinfo works fine... You can check out my phpinfo @ http://www.i2l2.com/phpinfo.php if that would provide you with some additional information to help me out... 8)

Thanks,
Pistolfire99

Posted: Thu May 23, 2002 5:50 pm
by volka
you should use phpinfo() in 'car.php' to see if the variable is set at all ;)

Posted: Thu May 23, 2002 6:20 pm
by pistolfire99
It works now,

Code: Select all

<?php
      if ( $age > 20 && $license =="on" )
               echo ("You can rent the car<br><br>");
      
      if ( $age < 21 || $license =="" )
               echo ("Sorry, You are denied");
?>
Rather than having two 'if' statements as per the book, I changed it to "if then else"

Code: Select all

<?php
    if ( $age > 20 && $license =="on" )
               echo ("You can rent the car<br><br>");
    else
              echo ("Sorry, You are denied");
?>
Now it works the way I want it to... Thanks for the help guys :D

Pistolfire99