lundi 3 août 2015

Passing an HTML Checkbox Array (w/ Empty Values) to PHP

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?




Aucun commentaire:

Enregistrer un commentaire