Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to get the array of checkboxes within a form array. I am trying to get the data as the form array first then get the array of checkboxes within the array.

<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;
        }
    }
}
share|improve this question
4  
.....And the problem is?? –  bivoc Mar 3 '13 at 0:37
    
the problem is there is no question mark in the question –  Thomas Haratyk Mar 3 '13 at 0:38
    
Downvoted for lack of question. –  Nathaniel Mar 3 '13 at 0:40
    
please consider to accept an answer (click tick mark on the left) if it actually answered your question –  michi Apr 14 '13 at 12:55

1 Answer 1

Mindreading: You get all checkbox-values across all categories but want them by category. --> you have to link the subcategories (checkboxes) to the categories by the name-attribute.

$categories: array holding id and name of your categories:

<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>

After submit:

if (isset($_POST['submit_preview'])) {

    foreach ( $_POST as $name => $value) {

        if (substr($name,0,4)=='cat_') echo "$name: $value <br />";

    } // foreach
} // if isset
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.