Search code examples
phparraysforeach-loop-container

Collect $_POSTs from multiple checkbox rows


I have a form with multiple rows of checkboxes, each with a specific id, that are being displayed using a foreach loop.

How do you grab the $_POST info from something like that? I think it is like this somehow $_POST[][], like a sub-array, but I cant figure out how to set it up:

foreach($stakholderArray as $currentEntry) {
    print "<tr class='$bgcolor'>";
    print "<td class='left'>$currentEntry[org]</td>";

    if($currentEntry['dataFound']) {  
        //if data was found for current stakeholder, display it
        print ($currentEntry['Partner']) ? '<td><input type ="checkbox" checked ="checked" /></td>' : '<td><input type ="checkbox"  /></td>';
        print ($currentEntry['Agreement']) ? '<td><input type ="checkbox" checked ="checked" /></td>' : '<td><input type ="checkbox"  /></td>';
        print ($currentEntry['Train']) ? '<td><input type ="checkbox" checked ="checked" /></td>'  : '<td><input type ="checkbox"  /></td>';
        print ($currentEntry['Meet'])  ? '<td><input type ="checkbox" checked ="checked" /></td>' : '<td><input type ="checkbox"  /></td>';
    }
    else {  //else...no stakeholder data, display empty columns
        print "<td><input type ='checkbox'/></td><td><input type ='checkbox'/></td><td><input type ='checkbox'/></td><td><input type ='checkbox'/></td><td><input type ='checkbox'/></td>";
        print "</tr>";
    }## Heading ##

Solution

  • it's somewhat related to a question i answered before: POST an array from an HTML form without javascript

    • encase them in a form
    • give them an "array" name
    • they end up as an array during post when submitted

    related items should have like this: name="item[collection name][collection name][]" - note the first indices pertaining the set (for easy location), and the empty index meaning in that set, there's an array (instead of single value). so for your check boxes:

    <input type="checkbox" name="answers[set1][]" value="apple" />   //imagine checked
    <input type="checkbox" name="answers[set1][]" value="orange" />  //imagine checked
    <input type="checkbox" name="answers[set1][]" value="grape" />
    <input type="checkbox" name="answers[set2][]" value="airplane" />   //imagine checked
    <input type="checkbox" name="answers[set2][]" value="train" />  //imagine checked
    <input type="checkbox" name="answers[set2][]" value="boat" />
    <input type="checkbox" name="answers[solo]" value="boar" /> //single type value. note that there is no [] in the end
    

    end up like this in the request array (like say POST):

    $_POST[] = array(
        'answers' => array(
            'set1' => array('apple','orange'),   //unchecked items won't be included
            'set2' => array('airplane','train'), //unchecked items won't be included
            'solo' => 'boar'
        )
    );
    

    <table>
        <?php foreach($stakeholderArray as $stakeholder): ?>
        <tr>
    
        <?php 
    
            //declare so these exist regardless of data
            $partner   = '';
            $agreement = '';
            $train     = '';
            $meet      = '';
    
            //if we have data, mark the boxes accordingly
            if($stakeholder['dataFound']){
    
                $checked = 'checked ="checked"';
    
                //mark as checked or blank
                $partner   = ($stakeholder['Partner'])   ? $checked: '';
                $agreement = ($stakeholder['Agreement']) ? $checked: '';
                $train     = ($stakeholder['Train'])     ? $checked: '';
                $meet      = ($stakeholder['Meet'])      ? $checked: '';
    
            }
        ?>
    
           <td><input value='partner' name="stake[<?= $stakeholder ?>][partner]" type ="checkbox" <?= $partner ?> /></td>
           <td><input value='agreement' name="stake[<?= $stakeholder ?>][agreement]" type ="checkbox" <?= $agreement ?> /></td>
           <td><input value='train' name="stake[<?= $stakeholder ?>][train]" type ="checkbox" <?= $train ?> /></td>
           <td><input value='meet' name="stake[<?= $stakeholder ?>][meet]" type ="checkbox" <?= $meet ?> /></td>
    
        </tr>
        <?php endforeach; ?>
    </table>
    

    they should end up like:

    $_POST[] = array(
        'stakeholder1' => array(
            'partner'=> 'partner',
            'agreement'=> 'agreement',
            'train'=> 'train',
            'meet'=> 'meet'
        ),
        'stakeholder2' => array(
            'partner'=> 'partner',
            'agreement'=> 'agreement',
            'train'=> 'train',
            'meet'=> 'meet'
        ),
    );