PHP error: Notice: Undefined index:

162,448

Solution 1

You're attempting to access indicies within an array which are not set. This raises a notice.

Mostly likely you're noticing it now because your code has moved to a server where php.ini has error_reporting set to include E_NOTICE. Either suppress notices by setting error_reporting to E_ALL & ~E_NOTICE (not recommended), or verify that the index exists before you attempt to access it:

$month = array_key_exists('month', $_POST) ? $_POST['month'] : null;

Solution 2

Are you putting the form processor in the same script as the form? If so, it is attempting to process before the post values are set (everything is executing).

Wrap all the processing code in a conditional that checks if the form has even been sent.

if(isset($_POST) && array_key_exists('name_of_your_submit_input',$_POST)){
//process form!
}else{
//show form, don't process yet!  You can break out of php here and render your form
}

Scripts execute from the top down when programming procedurally. You need to make sure the program knows to ignore the processing logic if the form has not been sent. Likewise, after processing, you should redirect to a success page with something like

header('Location:http://www.yourdomainhere.com/formsuccess.php');

I would not get into the habit of supressing notices or errors.

Please don't take offense if I suggest that if you are having these problems and you are attempting to build a shopping cart, that you instead utilize a mature ecommerce solution like Magento or OsCommerce. A shopping cart is an interface that requires a high degree of security and if you are struggling with these kind of POST issues I can guarantee you will be fraught with headaches later. There are many great stable releases, some as simple as mere object models, that are available for download.

Solution 3

Obviously $_POST['month'] is not set. Maybe there's a mistake in your HTML form definition, or maybe something else is causing this. Whatever the cause, you should always check if a variable exists before using it, so

if(isset($_POST['month'])) {
   $month = $_POST['month'];
} else {
   //month is not set, do something about it, raise an error, throw an exception, orwahtever
}

Solution 4

How I can get rid of it so it doesnt display it?

People here are trying to tell you that it's unprofessional (and it is), but in your case you should simply add following to the start of your application:

 error_reporting(E_ERROR|E_WARNING);

This will disable E_NOTICE reporting. E_NOTICES are not errors, but notices, as the name says. You'd better check this stuff out and proof that undefined variables don't lead to errors. But the common case is that they are just informal, and perfectly normal for handling form input with PHP.

Also, next time Google the error message first.

Solution 5

This are just php notice messages,it seems php.ini configurations are not according vtiger standards, you can disable this message by setting error reporting to E_ALL & ~E_NOTICE in php.ini For example error_reporting(E_ALL&~E_NOTICE) and then restart apache to reflect changes.

Share:
162,448
PHPNOOB
Author by

PHPNOOB

Updated on July 09, 2022

Comments

  • PHPNOOB
    PHPNOOB almost 2 years

    I am working on a shopping cart in PHP and I seem to be getting this error "Notice: Undefined index:" in all sorts of places. The error refers to the similar bit of coding in different places. For example I have a piece of coding that calculates a package price with the months a user decides to subscribe. I have the following variables where the errors refers to:

        $month = $_POST['month'];
        $op = $_POST['op'];
    

    The $month variable is the number the user inputs in a form, and the $op variable is different packages whose value are stored in a vriable that a user selects from radio buttons on the form.

    I hope that is clear in some way.

    Thank You

    EDIT: Sorry forgot to mention that they do go away when the user submits the data. But when they first come to the page it displays this error. How I can get rid of it so it doesnt display it?

    --

    This is the code:

    <?php
        $pack_1 = 3;
        $pack_2 = 6;
        $pack_3 = 9;
        $pack_4 = 12;
        $month = $_POST['month'];
        $op = $_POST['op'];
        $action = $_GET['action'];
    
        if ( $op == "Adopter" ) {
           $answer = $pack_1 * $month;
        }
    
        if ( $op == "Defender" ) {
          $answer = $pack_2 * $month;
        }
    
        if ( $op == "Protector" ) {
          $answer = $pack_3 * $month;
        }
    
        if ( $op == "Guardian" ) {
          $answer = $pack_4 * $month;
        }
    
        switch($action) {   
            case "adds":
                $_SESSION['cart'][$answer][$op];
                break;
        }
    ?>  
    
  • user229044
    user229044 over 13 years
    I personally avoid 'is_set', as it returns false if $_POST['month'] has been set to null. When you really mean to ask if the array key exists, use array_key_exists.
  • mario
    mario over 13 years
    @meagar: It's unlikely that any $_POST[] var contains a NULL if it originates from multipart/form-data. That would only be an issue if subfunctions inject state via $_POST, which should be way more worriesome then differentiating between null and unset.
  • DeaconDesperado
    DeaconDesperado over 13 years
    Judging from his last comment, I think he isn't checking first for form submission. The notices are raised because whatever processing is taking place on data that hasn't been sent yet.
  • mario
    mario over 13 years
    @DeaconDesperado: Likely. The one snippet looks as if the $_POST input vars are just localized at the start of the script no matter what. Processing might be triggered by another if or just boolean state of one of those variables. ... We'll never find out :}
  • DeaconDesperado
    DeaconDesperado over 13 years
    and so it will remain one of the great mysteries of our age... which won't stop it from being posted again and again and again lol.
  • PHPNOOB
    PHPNOOB over 13 years
    hi, yes the form is on the same page as the script, so i think that is one of the problem, putting the error reporting code has removed the error now, but im having problems with other bits of code, so i think i really need to restructure everything!
  • PHPNOOB
    PHPNOOB over 13 years
    Unfortunately i cant use any off the shelf cart as it is part of an assignemnt where we have to build a dynamic website in php where users can adopt animals etc, so have to program it from scratch, and its been made more complex by the way the lecturer wants the whole thing done,
  • DeaconDesperado
    DeaconDesperado over 13 years
    Ah, so then this would be homework. That's a bit of a finicky word around these parts lol. webforcecart.com
  • DeaconDesperado
    DeaconDesperado over 13 years
    Check out how adding and removing items is handled in that class. Should point you in the right direction.
  • Wrikken
    Wrikken about 11 years
    Due to newbees possible misunderstanding: $_POST is always set regardless of sapi, or whether it's a GET or POST (HEAD, PUT, ..), unless explicitly disabled in variables_order (PHP_INI_DIR since php 5.0.5, so if disables there it's probably always disabled for the file). Summarizing the above with only isset($_POST['name_of_your_submit_input']) is fine.
  • RandheerPratapSingh
    RandheerPratapSingh about 8 years
    Got two Errors 1) Undefined Index: parse_var 2) Undefined variable sent
  • clemens
    clemens over 6 years
    Please give a more detailed description of your solution.
  • Ajmal Tk
    Ajmal Tk over 6 years
    In my case the problem solved by replacing form tag; <td> <form id="idsent" name="idsent" method="POST" action="<?php echo site_url('controller/view');?>" > <input type="hidden" value="<?php echo $tbln;?>" name="slctdtbl" /> <input type="radio" id="idradio" name="idradio" value="<?php echo $row['id']; ?>"> </td> <td><?=$row['id'];?></td> <td> <?=$row['adrs'];?></td> <td> <?=$row['ownr'];?></td> <td> <?=$row['rmrk'];?></td> echo '</tr>'; } ?> </form> Like this; radio button </form> tag close after the looping block.
  • clemens
    clemens over 6 years
    You should edit your answer, and add the formatted source code. It's unreadable.