Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP error: Notice: Undefined index:

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;
    }
?>  
like image 384
PHPNOOB Avatar asked Dec 16 '10 21:12

PHPNOOB


People also ask

How do I fix undefined index in PHP?

To resolve undefined index error, we make use of a function called isset() function in PHP. To ignore the undefined index error, we update the option error_reporting to ~E_NOTICE to disable the notice reporting.

How do I get rid of notice undefined index?

You can get rid of the PHP undefined index notice by using the isset() function. The PHP undefined variable notice can be removed by either using the isset() function or setting the variable value as an empty string.

How fix undefined offset in PHP?

Fix Notice: Undefined offset by using isset() Function Check the value of offset array with function isset(), empty(), and array_key_exists() to check if key exist or not.

What is undefined in PHP?

To check if a variable is undefined you will have to check if the variable is in the list of defined variables, using get_defined_vars() . There is no equivalent to JavaScript's undefined (which is what was shown in the question, no jQuery being used there).


1 Answers

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;
like image 77
meagar Avatar answered Oct 08 '22 15:10

meagar