Jump to content

Newbie - Undefined variable error


mrlarge

Recommended Posts

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.
Link to comment
Share on other sites

[!--quoteo(post=357555:date=Mar 23 2006, 12:05 PM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ Mar 23 2006, 12:05 PM) [snapback]357555[/snapback][/div][div class=\'quotemain\'][!--quotec--]
If you parsing the variable [b]person[/b] 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.
Link to comment
Share on other sites

with the globals off you can do this

(Who_are_you.php)
[code]<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>[/code]

(You_are.php)
[code]<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>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.