Page 1 of 1
undefined index
Posted: Thu Oct 26, 2006 6:06 am
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
Re: undefined index
Posted: Thu Oct 26, 2006 6:37 am
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.
Posted: Thu Oct 26, 2006 2:30 pm
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?
Posted: Thu Oct 26, 2006 2:36 pm
by volka
What is $checkbox_arr?
Posted: Thu Oct 26, 2006 2:49 pm
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
Posted: Thu Oct 26, 2006 2:59 pm
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.
Posted: Thu Oct 26, 2006 3:00 pm
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' );
Posted: Thu Oct 26, 2006 3:08 pm
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.
Posted: Thu Oct 26, 2006 3:36 pm
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!
Posted: Thu Oct 26, 2006 3:40 pm
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' );
Posted: Thu Oct 26, 2006 3:48 pm
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.
Posted: Thu Oct 26, 2006 3:57 pm
by garry27
thanks for all the advice.