Question

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 ##
Was it helpful?

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

OTHER TIPS

Give a different name tag to every checkbox element (you need to add name="WhatEverYouwant")

and you will be able to get it by:

$_POST['ID Of the Element']

Example:

'<td><input type ="checkbox" name="new" checked ="checked" /></td>' : '<td><input type ="checkbox"  /></td>';

and get it by:

$_POST['new']

Assuming that you have that in a <form> already, you need to give each input an id. Then in the resulting PHP script, use $_POST['whatever_the_name_is'] (you could also use $_REQUEST or $_GET depending on your form action).

Not an answer, but yeowch... reduce some of the duplicate HTML in your logic:

print ($currentEntry['Partner'])     ? '<td><input type ="checkbox" checked ="checked" /></td>' : '<td><input type ="checkbox"  /></td>';

should be

<td><input type ="checkbox"<?php ($currentEntry['Partner'] ? ' checked ="checked"' : '' ?> /></td>

Change:

<input type ="checkbox" ...

To:

<input type="checkbox" name="stakeholderarray[]" ...

Or:

<input type="checkbox" name="stakeholderarray[KEYNAME]" ...

Access in PHP:

foreach($_POST['stakeholderarray'] as $this_stakeholderarray){
 ...
}

Or:

$_POST['stakeholderarray']['KEYNAME'];

This works because []/[KEYNAME] added to the end of the name attribute is seen as an array item in PHP, and can therefore be looped through. You can nest arrays this way as well, so if you want to have multiple stake holders on a single form, do something like this:

<input type="checkbox" name="stakeholderarray[0][0]" ... <!-- Holder 0, item 0 -->
<input type="checkbox" name="stakeholderarray[0][1]" ... <!-- Holder 0, item 1 -->
<input type="checkbox" name="stakeholderarray[1][0]" ... <!-- Holder 1, item 0 -->
<input type="checkbox" name="stakeholderarray[1][1]" ... <!-- Holder 1, item 1 -->
$i = 0;
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 '<td><input type ="checkbox" name="chkPartner['.$i.']" '.(($currentEntry['Partner'])?'checked ="checked"':'').' /></td>';
  //print the rest like this
  $i++;
}

You can then access them from $_POST like so

if(isset($_POST[chkPartner][$yourIndex]))
{}

Actualy id shouldn't work. As Joseph said, form elements are sent with their names as keys. So the proper tag should be:

<input type="checkbox" name="some_name" ... />

When you send the form, you can get the data like $_POST['some_name']

If you like you can put them in an array name="somearr[someotherarr[some_name]]]" then the content will be available in $_POST['somearr']['someotherar']['some_name']

Hope that helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top