我正在尝试在表单数组中获取复选框数组。我试图首先将数据作为表单数组,然后获取数组中的复选框数组。
<form name="preview" id="post" action="" method="post">
<input type="hidden" name="category[$id]" />
<input type="checkbox" name="subcat[]" value="something" />
<input type="checkbox" name="subcat[]" value="somewhere" />
<input type="checkbox" name="subcat[]" value="something" />
<input type="checkbox" name="subcat[]" value="somewhere" />
<input type="hidden" name="category[$id]" />
<input type="checkbox" name="subcat[]" value="something" />
<input type="checkbox" name="subcat[]" value="somewhere" />
<input type="checkbox" name="subcat[]" value="something" />
<input type="checkbox" name="subcat[]" value="somewhere" />
<input class="button-primary" name="submit_preview" value="Submit Selected Tees" type="submit">
</form>
if (isset($_POST['submit_preview'])) {
foreach ( $_POST[ 'category' ] as $id ) {
foreach ( $_POST[ 'subcat' ][$id] as $subcat ) {
echo $subcat;
}
}
}
答案 0 :(得分:0)
Mindreading:您可以获得所有类别的所有复选框值,但需要按类别进行操作。 - &GT;您必须通过name-attribute将子类别(复选框)链接到类别。
$categories
:您的类别中包含id
和name
的数组:
<form name="preview" id="post" action="" method="post">
<?
foreach ($categories as $cat) { // for each of your categories 4 checkboxes
echo "<p>category: {$cat['name']}</p>";
echo "<input type=\"checkbox\" name=\"cat_{$cat['id']}_sub[]\" value=\"something\" /> option 1<br />";
echo "<input type=\"checkbox\" name=\"cat_{$cat['id']}_sub[]\" value=\"something\" /> option 2<br />";
echo "<input type=\"checkbox\" name=\"cat_{$cat['id']}_sub[]\" value=\"something\" /> option 3<br />";
echo "<input type=\"checkbox\" name=\"cat_{$cat['id']}_sub[]\" value=\"something\" /> option 4<br />";
// name of checkbox if id=1: "cat_1_sub[]"
} // foreach
<input class="button-primary" name="submit_preview" value="Submit Selected Tees" type="submit">
</form>
提交后:
if (isset($_POST['submit_preview'])) {
foreach ( $_POST as $name => $value) {
if (substr($name,0,4)=='cat_') echo "$name: $value <br />";
} // foreach
} // if isset