But… the array you’re building isnt being used to do the in_array?
in_array($q[‘Id_Cntry’], $_REQUEST[‘countries’]))
$_REQUEST[‘countries’] comes from a form input; this form seems to use checkboxes with values, and i’m gonna guess it’s action is set to POST… which your function isnt checking for, it’s looking at GET.
Ok… (your post was being written as i made mine :P)
If you’ve put var_dump($_REQUEST[‘countries’]) right before the line that generates the checkboxes and gotten that result, then i’m as confused as you…
Probably going to need to see more of your code to spot the gotchas.
I have moved the var_dump as shown below, and there a before, during and after result with the checkboxes.
So this is the code -
$r=mysql_query("SELECT DISTINCT(Id_Cntry), Nom_Cntry_DE FROM tbl_countries LEFT JOIN tbl_hotels ON (tbl_countries.Id_Cntry=tbl_hotels.IdCntry_Hot) LEFT JOIN tbl_hotname ON (tbl_hotname.Id_Hot=tbl_hotels.Id_Hot) LEFT JOIN tbl_amenities ON (tbl_amenities.Id_Ame=tbl_hotname.Id_Ame) WHERE (tbl_hotels.Act_Hot='1') AND (tbl_amenities.Id_Ame='806') ORDER BY tbl_countries.Nom_Cntry_DE");
while($q=mysql_fetch_assoc($r)){ ?>
<input type="checkbox" name="countries[]" value="<?php echo $q['Id_Cntry']?>" onClick="javascript:checkRefresh()" <?php echo (isset($_REQUEST['countries']) && in_array($q['Id_Cntry'], $_REQUEST['countries'])) ? 'checked="checked"' : '' ?> class="inline" /><?php echo $q['Nom_Cntry_DE']?>
<?php var_dump($_REQUEST['countries']); ?>
And this is what I get before the checkbox is selected -
string(0) ""
When I select the checkbox -
array(1) { [0]=> string(1) "9" }
And then the same again after I de-select that checkbox -
Notice: Undefined index: countries in \\CSFFILES11\WEBSITES\live\new_checksafetyfirst\de\category_Result.php on line 452 NULL
I’m wondering if the javascript could be causing a problem, do you have firebug or en equivalent console browser plugin installed to be able to see what is being sent by the browser?
btw, are you aware that the old mysql_* extension which you’re using is deprecated in version 5.5 of PHP (the current version) and is being removed from version 7 (the next version). You need migrate over to using either the mysqli_* extension or PDO
Actually PHP 5.6 is the current version at the time I am writing this although PHP 7 is scheduled to become the current version some time today (correction: it hasn’t been announced anywhere I can see yet but PHP 7.0.0 GA can be downloaded from https://github.com/php/php-src/releases/tag/php-7.0.0 so obviously it already is the current version).
The mysql_ interface HAS been removed from PHP 7 and so now only exist in old versions of PHP.
I’m not surprised you get the error message when there are no checkboxes selected, because at that point the array does not exist. I thought the problem was that you were getting that error even though the array was being passed correctly.
You check for it existing with an isset, but surely you’ll need to put the next part (the in_array() call) inside that if? Using it as the second half of an AND check means that it will run the in_array even if the isset() fails, it has to in order to evaluate whether both are true. Maybe something like this, but I might have imbalanced brackets.