0

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');
?>

2 Answers2

0

Checkbox values only get sent if they are checked.

The checkbox name does not have to be an 'array' format, unless there are lots of them. In this case, each one will need a different value.

Not sure what all your code is doing. You do not seem to be checking anywhere for if ( is_array ( $_POST['cb'] ) ) { ...

MaggsWeb
  • 3,018
  • 1
  • 13
  • 23
  • I attempted to remove all irrelevant code to the issue. I am passing up to 20 checkboxes per array and have maybe 15 arrays. I have checkboxes that aren't checkbox arrays and I thought that would check them. I suppose I could have the JS create new boxes with a different values. If I do that, what will I need to do PHP side to recognize empty boxes? – howdoisysadmin Aug 03 '15 at 19:02
  • You need to generate a naming system for your checkboxes, and then a simple function to use your system to determine which checkboxes have been checked. – MaggsWeb Aug 03 '15 at 19:04
  • I just checked and I have 98 to 337 checkboxes. So I'm supposed to check each value individually to see if it's checked? There is no way to send or check for null values in a checkbox array otherwise? This is harsh. – howdoisysadmin Aug 03 '15 at 19:18
  • I think I get what you are doing, yet it still looks over complicated to me. I also can't see the advantage of the function if you are only ever calling it once. Have you thought of prefixing checkbox names with a constant string, and then looping through like `foreach($_POST as $key => $value)..` – MaggsWeb Aug 03 '15 at 21:59
  • Actually, it turns out that it works perfectly! I guess I was just having an issue with the JS I was using to generate more boxes. I am calling the function at least 20 times as I load different checkbox or checkbox arrays. Thank you so much for the help! – howdoisysadmin Aug 03 '15 at 22:13
-1

You could use hidden element to get unchecked value. Try something like this:

<input type="hidden" name="checkbox" value="unchecked" />
<input type="checkbox" name="checkbox" id="checkbox1" value="checked" />

When your checkbox is unchecked, then $_POST['checkbox'] value is from hidden element. Otherwise is from checkbox.

Styx
  • 1,303
  • 1
  • 19
  • 30
  • Can you guarantee the order that the form elements are sent, so that the correct value gets sent? – MaggsWeb Aug 03 '15 at 21:48
  • It's not directly said in HTML specs. Please look this answer: http://stackoverflow.com/a/4400824/1382645 In other hand that is done in Zend Framewor 1 which I use from years and didn't see any issues. – Styx Aug 03 '15 at 22:25
  • Thanks for the answer, I've seen this solution elsewhere however I didn't know how to apply it to an array. I now have a PHP function that parses the checkbox variables. – howdoisysadmin Aug 03 '15 at 22:26
  • Elements hidden and checkbox must have same name. – Styx Aug 03 '15 at 22:29