undefined index

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
garry27
Forum Commoner
Posts: 90
Joined: Sat Oct 14, 2006 1:50 pm

undefined index

Post by garry27 »

anybody know how to get rid of undefined index errors which occur when you pass input values with a nil value - as in the case with an unchecked checkbox?

TIA
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Re: undefined index

Post by volka »

garry27 wrote:when you pass input values with a nil value - as in the case with an unchecked checkbox?
An unchecked box generates no http parameter at all. Use isset() to test the existance of a parameter/variable/element.
garry27
Forum Commoner
Posts: 90
Joined: Sat Oct 14, 2006 1:50 pm

Post by garry27 »

i've tried using isset on the following code but it still doesn't remove the notices:

Code: Select all

for ( $i = 0; $i <10; $i++ )
   {
    $element = $chkbox_arr[$i];
    if (!isset ( $_POST[$element]))
 	  unset($chkbox_arr[$i]);
   }
is this because element is a local variable- please could you show what i'm doing wrong?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

What is $checkbox_arr?
tbrown1
Forum Newbie
Posts: 17
Joined: Wed Oct 25, 2006 10:58 am

Post by tbrown1 »

$chkbox_arr[$i]; is a call to the array created by <input type="checkbox" name=chkbox_arr[]> then your issue is that if a checkbox is not check it is not added to the array
ie:
if you have 30 check boxes that all have name=chkbox_arr[] and only 3 of those are checked then the array is only 3 long no 30 long
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Where in the code is the undefined index notice being thrown? Typically this referes to you referencing an array by using an index that is not in the array AND not checking to see if it is set first.
garry27
Forum Commoner
Posts: 90
Joined: Sat Oct 14, 2006 1:50 pm

Post by garry27 »

$chk_box array is actually an array of the checkbox values which are passed on to the next page, which i created. obviously any boxes that are not checked do not return anhthing which is where the dilema lies.

Code: Select all

$chkbox_arr = array ( 'dj', 'liveMusic', 'nsArea','food','skySport', 'cocktails'
						 ,'pool', 'garden', 'lateLice', 'kidsZone' );
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

So you have a checkbox value array which builds your check boxes, right? Then you pass the form information and check each checkbox value right? Why not pass the checkbox array indeces along with it?

Code: Select all

<?php
$checkbox_array = array(
    'dj', 
    'liveMusic', 
    'nsArea',
    'food',
    'skySport', 
    'cocktails',
    'pool', 
    'garden', 
    'lateLice', 
    'kidsZone'
);
$checkbox_count = count($checkbox_array);
for ($i = 0; $ < $checkbox_count; ++$i)
{
    echo '<input type="checkbox" name="check_this[' . $i . ']" />';
}
?>
Then when they are passed, check their set the same way...

Code: Select all

<?php
foreach ($_POST['check_this'] as $key => $value)
{
    if (isset($value))
    {
        echo $checkbox_array[$key] . ' was set...';
    }
}
?>
Or something along those lines. This is untested, but I think it might be usable in some capacity.
garry27
Forum Commoner
Posts: 90
Joined: Sat Oct 14, 2006 1:50 pm

Post by garry27 »

no, i'm using pure html to build the checkboxes and i'm using the value of the checkboxes both as an index and a return value. basically if it returns a value then they're checked and if it doesn't then their not and i erase them from chkbox_arr for later use.

this function works but i tcauses index notice errors, such as:

Notice: Undefined index: livemusic in /home/unn_p921847/public_html/create_crawl.php on line 18

Notice: Undefined index: playarea in /home/unn_p921847/public_html/create_crawl.php on line 18

Notice: Undefined index: latelicence in /home/unn_p921847/public_html/create_crawl.php on line 18


findpubs.html sends the checkbox values to create_crawl.php and create_crawl.php calls on another script which contains the function i've described. phew!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

array indices are case-sensitive.
garry27 wrote:Notice: Undefined index: latelicence in /home/unn_p921847/public_html/create_crawl.php on line 18
garry27 wrote:$chkbox_arr = array ( 'dj', 'liveMusic', 'nsArea','food','skySport', 'cocktails'
,'pool', 'garden', 'lateLice', 'kidsZone' );
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

garry27 wrote:no, i'm using pure html to build the checkboxes
If you let PHP build them and check them using the same array, case-crash issues would probably never happen... that is just a suggestion, not a solution.

The thing to remember with checkboxes is they pass as either 'on' or empty (unset). So you can't assign a value from an unchecked checkbox. You might want to look at in_array() to see if the values of the passed checkboxes are in the known checkbox array and assign from that. But trying to remove something that PHP never knows is there might cause problems.
Last edited by RobertGonzalez on Thu Oct 26, 2006 4:02 pm, edited 1 time in total.
garry27
Forum Commoner
Posts: 90
Joined: Sat Oct 14, 2006 1:50 pm

Post by garry27 »

thanks for all the advice.
Post Reply