I’m having issues passing HTML checkbox arrays to PHP with similar code to:
$cbArray = checkbox($_POST['cbArray']);
$cbArray = array_chunk($cbArray, 4);
$id = $_POST['id'];
$idcount = count($id);
$id = array_chunk($id, 4);
$cbOuterArrays = 0;
if( $idcount > 16 ){
$cbOuterArrays = 4;
}elseif( $idcount > 12 ){
$cbOuterArrays = 3;
}elseif( ($idcount > 8 ){
$cbOuterArrays = 2;
}elseif( $idcount > 4 ){
$cbOuterArrays = 1;
}
for( $i = 0; $i <= $cbOuterArrays; $i++ ){
$c = 0;
if(isset($id[$i][$c])){
if($cb[$i][$tc] == 'x'){
echo "1st checked in chunk " . $c;
}
}
$c++
if(isset($id[$i][$c])){
if($cb[$i][$tc] == 'x'){
echo "2st checked in chunk ". $c;
}
}
$c++
if(isset($id[$i][$c])){
if($cb[$i][$tc] == 'x'){
echo "3st checked in chunk . $c;
}
}
$c++
if(isset($id[$i][$c])){
if($cb[$i][$tc] == 'x'){
echo "4st checked in chunk . $c;
}
}
}
?>
My HTML is something similar to this:
<html>
<input type="text" name="id[]">
<input type="checkbox" name="cb[]"> //I have tried value="1" as well but it didn't help
//Then I have a button that runs js to add more checkboxes
<input type="submit" value="Submit">
</html>
No matter which checkbox I check, if I only check one it echos "1st checked in chunk 0". It's almost as if it's pulling in only populated values. Sorry if the code looks bad. I attempted to remove everything irrelevant while leaving anything that could be the cause of the issue. I thought my checkbox function would check each value and if it's empty make it a blankspace in the array. How do I get it to recognize empty checkboxes?
EDIT:
As MaggsWeb stated I cannot send empty checkboxes. I changed my checkbox function to:
<?php
function cb($x){
$a = []; #Create new array
$cv = 0; #Current new array value
foreach($_POST[$x] as &$v){ #Loop through the first array
while($cv < intval($v)){ #If the new array is not at the old arrays value loop
$a[$cv] = ' '; #Fill the new array value with blank space
$cv++; #Move to next value in new array
} #Repeat if not at the value yet
$a[$cv] = 'x'; #Set the current value of new array to 'x'
$cv++; #Move to next value in new array
} #Move to next value in old array & Repeat
return $a; #Return the new array
}
#and call it via cb('id');
?>