RE: [PHP] Notice: Undefined index
| From: | Mike Johnson | Date: | Thu, 28 Apr 2005 18:56:58 +0000 |
| Subject: | RE: [PHP] Notice: Undefined index | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-214136@lists.php.net to get a copy of this message | ||
From: Rob Kapfer [mailto:rkapfer@comcast.net]
> Hello, This is a new install of PHP 5.0.4 on XP, I'm testing if it's
> installed correctly with the usual test.htm:
> <form action="action.php" method="POST">
>
> Your name: <input type="text" name="name" />
>
> Your age: <input type="text" name="age" />
>
> <input type="submit">
>
> </form>
>
> Action.php:
>
> <?php
>
> echo $_POST['name'];
>
> echo $_POST['age'];
>
> ?>
>
> Results:
>
> C:\NCC>action.php
>
> Notice: Undefined index: name in C:\NCC\action.php on line 2
>
> Notice: Undefined index: age in C:\NCC\action.php on line 3
>
> C:\NCC>
That's correct behavior. As you're simply running action.php on its own,
it sees that $_POST['name'] and $_POST['age'] don't exist and outputs a
notice for each.
To avoid the notices, ensure that those keys exist before calling
something like echo on them:
<?php
if (!empty($_POST['name'])) {
echo $_POST['name'];
} else {
echo 'Name not set';
}
if (!empty($_POST['age'])) {
echo $_POST['age'];
} else {
echo 'Age not set';
}
?>
HTH!
--
Mike Johnson Smarter Living, Inc.
Web Developer www.smartertravel.com
mike@smarterliving.com (617) 886-5539