p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: Errors on chapter 4 example


Message #1 by "Lonnie Minter" <lminter@f...> on Wed, 16 Oct 2002 23:26:44
I'm doing the holiday form example and I keep getting an 'undefined 
variable' error. The php script is supposed to recognize that nothing was 
selected and then post a message saying to go back and do it again. Well, 
it does this but it also gives me the errors.

php script:
<?
$price=500;
$starModifier=1;
$cityModifier=1;
if ($grade=="three")
{
	if ($destination=="Barcelona")
	{
		$cityModifier=2;
		$price=$price*$cityModifier;
		echo "The cost for a week in $destination is $price";
	}
	elseif ($destination=="Vienna")
	{
		$cityModifier=3.5;
		$price=$price*$cityModifier;
		echo "The cost for a week in $destination is $price";
	}
	elseif ($destination=="Prague")
	{
		$price=$price*$cityModifier;
		echo "The cost for a week in $destination is $price";
	}
	else
	{
		echo ("You've not entered a value for dstination, go back 
and do it again");
	}
}
elseif ($grade=="four")
{
	$starModifier=2;
	if ($destination=="Barcelona")
	{
		$cityModifier=2.5;
		$price=$price*$cityModifier*$starModifier;
		echo "The cost for a week in $destination is $price";
	}
	elseif ($destination=="Vienna")
	{
		$cityModifier=4;
		$price=$price*$cityModifier*$starModifier;
		echo "The cost for a week in $destination is $price";
	}
	elseif ($destination=="Prague")
	{
		$price=$price*$cityModifier*$starModifier;
		echo "The cost for a week in $destination is $price";
	}
else
{
	echo ("You've not entered a value for destination, go back and do 
it again");
	}
}
else
{
	echo ("You've not entered a value for hotel grade, go back and do 
it again");
}
?>


When I fill out nothing n the form and submit,it says this:

Namlu Holiday Booking Form 
Notice: Undefined variable: grade in c:\inetpub\wwwroot\php\holiday.php on 
line 9

Notice: Undefined variable: grade in c:\inetpub\wwwroot\php\holiday.php on 
line 33
You've not entered a value for hotel grade, go back and do it again 

Any ideas?
Message #2 by "Nikolai Devereaux" <yomama@u...> on Wed, 16 Oct 2002 15:42:44 -0700
The problem you're experiencing is because you're accessing a variable that
isn't set.  If $grade isn't defined, then comparing $grade and "three" will
throw an undefined variable warning.

You can suppress this warning by changing the error_reporting level in php.ini
to E_ALL & !E_NOTICE.


There's a bunch of info about this in the archives -- search for
"error_reporting" or "E_NOTICE" to get some results.

Also, read this section of the manual:
  http://www.php.net/security

more specifically:
  http://www.php.net/security.errors


take care,

nik

Message #3 by "Bill Lewis" <tekronx@a...> on Wed, 16 Oct 2002 19:33:27 -0400
Did you enable the register_globals in the php.ini?

-----Original Message-----
From: Lonnie Minter [mailto:lminter@f...]
Sent: Wednesday, October 16, 2002 11:27 PM
To: beginning php
Subject: [beginning_php] Errors on chapter 4 example


I'm doing the holiday form example and I keep getting an 'undefined
variable' error. The php script is supposed to recognize that nothing was
selected and then post a message saying to go back and do it again. Well,
it does this but it also gives me the errors.

php script:
<?
$price=500;
$starModifier=1;
$cityModifier=1;
if ($grade=="three")
{
	if ($destination=="Barcelona")
	{
		$cityModifier=2;
		$price=$price*$cityModifier;
		echo "The cost for a week in $destination is $price";
	}
	elseif ($destination=="Vienna")
	{
		$cityModifier=3.5;
		$price=$price*$cityModifier;
		echo "The cost for a week in $destination is $price";
	}
	elseif ($destination=="Prague")
	{
		$price=$price*$cityModifier;
		echo "The cost for a week in $destination is $price";
	}
	else
	{
		echo ("You've not entered a value for dstination, go back
and do it again");
	}
}
elseif ($grade=="four")
{
	$starModifier=2;
	if ($destination=="Barcelona")
	{
		$cityModifier=2.5;
		$price=$price*$cityModifier*$starModifier;
		echo "The cost for a week in $destination is $price";
	}
	elseif ($destination=="Vienna")
	{
		$cityModifier=4;
		$price=$price*$cityModifier*$starModifier;
		echo "The cost for a week in $destination is $price";
	}
	elseif ($destination=="Prague")
	{
		$price=$price*$cityModifier*$starModifier;
		echo "The cost for a week in $destination is $price";
	}
else
{
	echo ("You've not entered a value for destination, go back and do
it again");
	}
}
else
{
	echo ("You've not entered a value for hotel grade, go back and do
it again");
}
?>


When I fill out nothing n the form and submit,it says this:

Namlu Holiday Booking Form
Notice: Undefined variable: grade in c:\inetpub\wwwroot\php\holiday.php on
line 9

Notice: Undefined variable: grade in c:\inetpub\wwwroot\php\holiday.php on
line 33
You've not entered a value for hotel grade, go back and do it again

Any ideas?


  Return to Index