RE: Notice: Undefined index

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: Rob Kapfer [mailto:rkapfer@xxxxxxxxxxx] 

> 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@xxxxxxxxxxxxxxxxx   (617) 886-5539

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux