Undefinded index using $_POST ...

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

Hello, I'm just getting starting in php and MySQL and I've got my
application working correctly on my local windows XP computer. Now, when I
publish to my ISP it's not going so well, which is also a Windows server.

I'm using form variables to control navigating to different pages when a
page is reloaded. As I said no problem on my machine but on the ISP server
I'm getting the following messages snd of course it's not working. Both
systems are running php version 4.3.10.

Thank you for any help provided because I'm really stumped!


Notice: Undefined index: Home in
d:\html\users9999\mydomaincom\html\myfolder\Member_Page.php on line 11
Notice: Undefined index: Clear in
d:\html\users9999\mydomaincom\html\myfolder\Member_Page.php on line 14
Notice: Undefined index: Update in
d:\html\users9999\mydomaincom\html\myfolder\Member_Page.php on line 16
Notice: Undefined variable: button in
d:\html\users9999\mydomaincom\html\myfolder\Member_Page.php on line 24
Notice: Undefined index: do in
d:\html\users9999\mydomaincom\html\myfolder\Member_Page.php on line 187

if ($_POST['Home'])
    $button = "Home"; }
   else if ($_POST['Clear']) {
    $button = "Clear"; }
   else if ($_POST['Update']) {
    $button = "Update"; }

  switch ($button)
    case "Clear":
        if (isset($selectedRow))
            unset ($selectedRow);
        $items = 1;
        $street2 = " ";
        $comments= " ";
        $searchState = "Oregon";
        $message_new = "New Item? Please complete following fields:";

    case "Home":
        // Load the Home Page
        $_SESSION['logname'] = $newname;
        header("Location: index.php");

    case "Update":

      foreach($_POST as $field => $value)
        if ($field != "email" and $field !="street2" and $field

Re: Undefinded index using $_POST ...

Vic Spainhower wrote:

Hi Vic,

Quoted text here. Click to load it

Alway use:
if (isset($_POST["home"]))

to check if a a variable (in an array or not) exists.

I think if you rewrite your code accordingly, you are out of notices.

Good luck!

Erwin Moller

Re: Undefinded index using $_POST ...


Thank you very much that was the problem!  Would it be the error reporting
level is different between my system and my ISP's server which is why it
appeared to work on my system?



"Erwin Moller"
Quoted text here. Click to load it

Re: Undefinded index using $_POST ...

 .oO(Vic Spainhower)

Quoted text here. Click to load it

Yep. The default error_reporting doesn't show notices. You should set it
to E_ALL in your php.ini.


Re: Undefinded index using $_POST ...

Erwin Moller wrote:

Quoted text here. Click to load it

....and to go further, you can wrap it:

function PostSafe($key,$default="",$report_bad_key=true) {
 if (isset($_POST[$key])) { return MySanitizer($_POST[$key]); }
 if ($report_bad_key) {
  ErrorAdd("Attempt to retrieve non-existent post key: $key");
 return $default;

so a normal call is:

$home = PostSafe("Home");

and you can also provide a default and suppress error reporting on optional

$myvar = PostSafe("optional_variable","default_value",false);

Kenneth Downs
Secure Data Software, Inc.

Re: Undefinded index using $_POST ...

Quoted text here. Click to load it

Just stick a @ in front of $_POST[] to supress the notice:

if(@$_POST['Home']) {

Re: Undefinded index using $_POST ...

Chung Leong wrote:

Quoted text here. Click to load it

Yes, it supresses the error, but I do not like that approach.
IMHO it is better to branch your code, so you know if the posting contains
the things you expect.

But it will depend on the situation if you get away with it.

(Just my 2 cents, I am not saying you are wrong.)

Erwin Moller

Re: Undefinded index using $_POST ...

from Erwin Moller contained the following:

Quoted text here. Click to load it

I suspect Chung is being playful
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker /

Re: Undefinded index using $_POST ...

"Erwin Moller"
Quoted text here. Click to load it

Notices and warnings are not error. They're advisory messages that indicate
the *possibility* of a programming error. If triggered by something that
done by design, then they should simply be ignored. There is no point in
coding around false positives.

In this case, the absence of the field 'Home' is expected (hence the if
statement), so it is not a programming error.

Site Timeline