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 ##
Not an answer, but yeowch... reduce some of the duplicate HTML in your logic:
should be
it's somewhat related to a question i answered before: POST an array from an HTML form without javascript
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:end up like this in the request array (like say POST):
they should end up like:
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).Change:
To:
Or:
Access in PHP:
Or:
This works because
[]
/[KEYNAME]
added to the end of thename
attribute is seen as anarray
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:You can then access them from
$_POST
like soGive a different
name
tag to everycheckbox
element (you need to addname="WhatEverYouwant"
)and you will be able to get it by:
Example:
and get it by: