undefined index
Moderator: General Moderators
undefined index
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
TIA
Re: undefined index
An unchecked box generates no http parameter at all. Use isset() to test the existance of a parameter/variable/element.garry27 wrote:when you pass input values with a nil value - as in the case with an unchecked checkbox?
i've tried using isset on the following code but it still doesn't remove the notices:
is this because element is a local variable- please could you show what i'm doing wrong?
Code: Select all
for ( $i = 0; $i <10; $i++ )
{
$element = $chkbox_arr[$i];
if (!isset ( $_POST[$element]))
unset($chkbox_arr[$i]);
}$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
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
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
$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' );- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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?
Then when they are passed, check their set the same way...
Or something along those lines. This is untested, but I think it might be usable in some capacity.
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 . ']" />';
}
?>Code: Select all
<?php
foreach ($_POST['check_this'] as $key => $value)
{
if (isset($value))
{
echo $checkbox_array[$key] . ' was set...';
}
}
?>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!
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!
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
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.garry27 wrote:no, i'm using pure html to build the checkboxes
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.