Jump to content


Photo

Newbie - Undefined variable error


  • Please log in to reply
3 replies to this topic

#1 mrlarge

mrlarge
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 23 March 2006 - 11:24 AM

Hi Guys.

Yep, am a newbie and the second bit of code I write wont work (the first was helloworld - would be difficult to not get that working!)
Anyway, the code I am using is:

(Who_are_you.php)

<html>
<head>
<title>Who Are You?</title>
</head>
<body>
<form action="you_are.php">
Please enter your name:<br />
I am...
<?php print('<input type="text" name="person" value="' . $person .
'"size="15" />');
?>
<input type="submit" value="Go!" size="15" />
</form>
</body>
</html>

(You_are.php)

<html>
<head>
<title>You Are ...</title>
</head>
<body>
<?php
print('Well, hello ' . $person . ', nice to meet you!');
print('<br />');
print('<a href="who_are_you.php?person=' . urlencode($person) . '">
Back to who are you?</a>');
?>
</body>
</html>

The output I get is:

(Who are you.php):

Please enter your name:
I am...
Notice: Undefined variable: person in c:\Inetpub\wwwroot\PHP-tests\Mastering PHP 4.1\Listing 1.3\who_are_you.php on line 9

NB. It does display the textbox and button, and when I submit I get:

Notice: Undefined variable: person in c:\Inetpub\wwwroot\PHP-tests\Mastering PHP 4.1\Listing 1.3\YOU_ARE.php on line 7
Well, hello , nice to meet you!

Notice: Undefined variable: person in c:\Inetpub\wwwroot\PHP-tests\Mastering PHP 4.1\Listing 1.3\YOU_ARE.php on line 9
Back to who are you?

the value $person is passed over in the url:

[a href=\"http://mysql_server/PHP-tests/Mastering%20PHP%204.1/Listing%201.3/you_are.php?person=nick\" target=\"_blank\"]http://mysql_server/PHP-tests/Mastering%20...php?person=nick[/a]

Oh, and I am using PHP 5.1.2 on IIS5 (Win XP Professional).

Nick.

Hope someone can help.
Regards,
Nick.


#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 23 March 2006 - 12:05 PM

If you parsing the variable person over the url then you will want to use the $_GET superglobal array instead, ie $_GET['person'];

This is because you have register_globals turn off. So you will need to use the superglobal array instead.

#3 mrlarge

mrlarge
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 23 March 2006 - 02:39 PM

[!--quoteo(post=357555:date=Mar 23 2006, 12:05 PM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ Mar 23 2006, 12:05 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
If you parsing the variable person over the url then you will want to use the $_GET superglobal array instead, ie $_GET['person'];

This is because you have register_globals turn off. So you will need to use the superglobal array instead.
[/quote]

Thanks for letting me know that the cause was register_globals was off. I turned it on and it worked. However, this does lead to security issues as stated in the narratives in php.ini, but the code I am going through is research anyway so for the time being that doesn't matter. I will also look at superglobal arrays at some point as well. I tried the $_GET['person'] in my code and it still didn't work until I turned register_globals on. Could you please post the alternative code to me (Ie. Your version with the $_GET change) so I can see what I have to do in future if I intend to leave register_globals turned off.

Thanks,
Nick.

#4 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 23 March 2006 - 02:47 PM

with the globals off you can do this

(Who_are_you.php)
<html>
<head>
<title>Who Are You?</title>
</head>
<body>
<form action="you_are.php" method="GET">
Please enter your name:<br />
I am...
<?
if(isset($_GET['person'])){
$person = $_GET['person'];
} else {
$person = "";
}
print('<input type="text" name="person" value="' . $person .
'"size="15" />');
?>
<input type="submit" value="Go!" size="15" />
</form>
</body>
</html>

(You_are.php)
<html>
<head>
<title>You Are ...</title>
</head>
<body>
<?php
$person = $_GET['person'];
print('Well, hello ' . $person . ', nice to meet you!');
print('<br />');
print('<a href="who_are_you.php?person=' . urlencode($person) . '">
Back to who are you?</a>');
?>
</body>
</html>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users