Page 1 of 1

Undefined Index Rate Topic: -----

#1 andrewsw   User is offline

  • no more Mr Potato Head
  • member icon

Reputation: 6957
  • View blog
  • Posts: 28,696
  • Joined: 12-December 12

Posted 16 August 2015 - 05:36 AM

This is an error that is encountered often in PHP, which I will explain and then give some guidance about how to solve.

PHP arrays associate keys with values. In the following
$array = array(
    "foo" => "bar",
    "doo" => "dah",

"foo" is the key for the value "bar".

If an explicit key isn't defined then incremental integers are used:
$array = array("foo", "bar", "hello", "world");

// output
array(4) {
  string(3) "foo"
  string(3) "bar"
  string(5) "hello"
  string(5) "world"

These numbers are indexes into the array.

However, because both the index-number and a specific key allow us to identify (to index/ refer to) an element within an array, the error message states "undefined index" in both cases.

The error message means that the element you are trying to refer to does not exist in the array. Either the key is misspelt, perhaps the array is empty, or typically the value (with that name) hasn't been posted successfully. The error message will also tell you specifically which key (which index) it cannot find and the line number that generated the error.

You will most often encounter this error when trying to read data in the $_POST or $_GET global array. Let's take a step back though, and revisit the HTML in-line with my sister tutorial Progressive Testing.


Validate your HTML to ensure that it is correctly formed.

If you are using a form (as opposed to an Ajax call or a-link) to send data then ensure that:

  • The form tag has method and action attributes.
  • You are using the corresponding method, "POST" or "GET", for the array you are reading.
  • If uploading a file you need enctype="multipart/form-data" as one of the form's attributes.
  • All values that you wish to send have the 'name' attribute. Form elements without this attribute won't be sent.
  • The names should be unique for each element, other than for radio buttons.
  • However, elements can have the same name if ended with square brackets "samename[]". Remember that the values will be received by PHP as an array.

The simplest, quickest, and most important, check that you can make is that the names you are using in the PHP correspond to the form element-names as they appear in the HTML. Also remember that PHP variables and array keys are case sensitive.

Javascript or jQuery

If using Ajax to send data, check your browser's console for error messages.

Pepper your code with console.log() calls, even just console.log("In success callback..") to check that all the parts of your code are being reached.

console.log() the data you are sending at the line just before sending, to check that it is correctly formed (and not empty).

If using Javascript, rather than jQuery, you need request.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); before sending data.

In success (or done) or onreadystatechange, console.log() the received data and the values of readyState and status.

Simple A-link

For a simple link it should look like

Once you've clicked the link look at what appears in the addressbar to check that it is correct. You call also edit the address directly in the addressbar for further testing.

The content after the question mark is the querystring and populates the $_GET array.

It is possible to POST data but to also attach values to the target URL at the same time. In which case, the posted data is in $_POST and the URL values are in $_GET.


The simplest test in the PHP is:
echo "here";

This confirms that PHP is working and the page is found. If using Ajax this value will populate the data received in the onreadystatechange event (or success callback); use console.log() to confirm that the value is received.

In the PHP change the echo to echo $_POST; or print_r($_POST);. Also echo $_GET, you might as well echo both in case you discover that the value(s) you are looking for are in the wrong array. Echo $_FILES if appropriate.

If the value you are struggling with appears in the $_POST or $_GET array then post it back, or print it, using echo $_POST['thename'];. This may seem redundant if using Ajax, but it is a useful check (and technique) to use: confirming that a value can be sent, read by PHP, and returned.

If you are still having problems then echo the $_POST array at the top of your script and just before the line that caused the "undefined index", to check that it isn't somehow being unset. Also check your PHP error logs.

If the problem still isn't solved then you'll need to apply imagination and persistence to the task. Nonetheless, you can still utilise the step-by-step approach I have encouraged. At this stage it is important to remain open-minded about where the problem is. Don't assume that "the Javascript is perfect, the problem is in the PHP", or vice versa. The problem, and solution, has eluded you, so maybe you've missed something when checking the JS.

This post has been edited by andrewsw: 16 August 2015 - 03:46 PM

Is This A Good Question/Topic? 0
  • +

Page 1 of 1