2013-03-03 57 views
-2

我想獲取表單數組中的複選框數組。我試圖首先獲取數據作爲表單數組,然後獲取數組中的複選框數組。表單數組中的複選框數組

<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; 
     } 
    } 
} 
+4

.....而問題是? – samayo 2013-03-03 00:37:46

+0

問題是在問題 – jazzytomato 2013-03-03 00:38:54

+0

Downvoted沒有問題沒有問號。 – Nathaniel 2013-03-03 00:40:59

回答

0

Mindreading:您將獲得所有類別的所有複選框值,但希望通過類別選擇它們。 - >必須通過名稱屬性將子類別(複選框)鏈接到類別。

$categories:列保持您的各類idname

<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