tags:

views:

425

answers:

4

Hi,

I have a checkbox group in one form that I need to be posted.

<input type="checkbox" value="true" checked name="chk0[]">
<input type="checkbox" value="false" name="chk0[]">
<input type="checkbox" value="false" name="chk0[]">
<input type="checkbox" value="true" checked name="chk0[]">

<input type="checkbox" value="true" checked name="chk1[]">
<input type="checkbox" value="false" name="chk1[]">
<input type="checkbox" value="true" checked name="chk1[]">
<input type="checkbox" value="false" name="chk1[]">

Note that in the first group, 1 and 4 are checked. In the second group, 1 and 3 are checked. When I post this, I get the posted values for the checked checkboxes sequentially as under:

[chk0] => Array ( 
    [0] => true 
    [1] => true 
  ) 
[chk1] => Array ( 
    [0] => true 
    [1] => true 
  )

How can I make sure I get the posted values like this:

[chk0] => Array ( 
    [0] => true 
    [3] => true 
  ) 
[chk1] => Array ( 
    [0] => true 
    [2] => true 
  )

I wanted to know the keys of the checked checkboxes instead of showing me sequentially.

Thanks

A: 

Explicitly provide your keys.

<input type="checkbox" value="true" checked name="chk0[0]">
<input type="checkbox" value="false" name="chk0[1]">
<input type="checkbox" value="false" name="chk0[2]">
<input type="checkbox" value="true" checked name="chk0[3]">

<input type="checkbox" value="true" checked name="chk1[0]">
<input type="checkbox" value="false" name="chk1[1]">
<input type="checkbox" value="true" checked name="chk1[2]">
<input type="checkbox" value="false" name="chk1[3]">

I would suggest bringing them into a single array, rather than two different ones:

<input type="checkbox" value="true"  name="chk[0][0]" checked>
<input type="checkbox" value="false" name="chk[0][1]">
<input type="checkbox" value="false" name="chk[0][2]">
<input type="checkbox" value="true"  name="chk[0][3]" checked>

<input type="checkbox" value="true"  name="chk[1][0]" checked>
<input type="checkbox" value="false" name="chk[1][1]">
<input type="checkbox" value="true"  name="chk[1][2]" checked>
<input type="checkbox" value="false" name="chk[1][3]">

Producing the following output:

Array
(
    [chk] => Array
        (
            [0] => Array
                (
                    [0] => true
                    [3] => true
                )

            [1] => Array
                (
                    [0] => true
                    [2] => true
                )

        )

)
Jonathan Sampson
+1  A: 
<input type="checkbox" value="true" checked name="chk0[0]">
<input type="checkbox" value="false" name="chk0[1]">
<input type="checkbox" value="false" name="chk0[2]">
<input type="checkbox" value="true" checked name="chk0[3]">

<input type="checkbox" value="true" checked name="chk1[0]">
<input type="checkbox" value="false" name="chk1[1]">
<input type="checkbox" value="true" checked name="chk1[2]">
<input type="checkbox" value="false" name="chk1[3]">

See what I mean?
The <input name="chk0[1]" ...> boxes work just like array elements inside PHP. You can specify the indexes an they will be transfered as-is into PHP as $_POST['chk0'][1] elements.

Atli
Why the down-vote? Did I miss something?... *(Hate it when people down-vote and then just run of without a word >.<)*
Atli
A: 

use the key in the name of the checkbox as in name="chk[3]" instead of name="chk[]"

A: 

I agree with Answer 1, but the issue is, I am using a parent checkbox to toggle the subcategory checkboxes. If I go with chk0[0],chk0[1], etc, the toggleParentCheckboxes function fails... Please take a look at this:

<html>
<head>

<script src="http://www.google.com/jsapi"&gt;&lt;/script&gt;

<script>
google.load("jquery", "1.3.2");
google.load("jqueryui", "1.7.2");
</script>

<script language="JavaScript">
 function toggleTableRows()
 {
     $(document).ready(function() {
        $('img.parent')
           .css("cursor","pointer")
           .toggle(
              function() {
                 $(this).attr("title","Click to Collapse")
                 $(this).attr("src","arrow_expanded.gif");
                 $('tr').siblings('#child-'+this.id).toggle();
              },
              function() {
                 $(this).attr("title","Click to Expand");
                 $(this).attr("src","arrow_collapsed.gif");
                 $('tr').siblings('#child-'+this.id).toggle();
              }
          );

          initCheckBoxes();
  });
}

function toggleCheckboxes(current, form, field) {
        $("#"+ form +" :checkbox[name='"+ field +"[]']").attr("checked", current.checked);
}

function toggleParentCheckboxes(current, form) {
        var checked = ($("#"+ form +" :checkbox[name='"+ current.name +"']").length == $("#"+ form +" :checkbox[name='"+ current.name +"']:checked").length);
        $("#"+ form +" :checkbox[name='"+ current.name.replace("[]", "") +"']").attr("checked", checked);
}

function initCheckBoxes() {
    $("#frmDinnerMenu :checkbox:checked").each(function() {
     if (this.name.replace(/[0-9]/g, "") == "chk[]") {
      toggleParentCheckboxes(this, "frmDinnerMenu");
     }
    });
}
</script>
<script language="JavaScript">toggleTableRows();</script>
</head>
<body>

<form name="frmDinnerMenu" id="frmDinnerMenu" method="POST" action="">
<table border=1>
<tr>
    <td><img class="parent" id="0" src="arrow_collapsed.gif" title="Click to Expand">Category - Fruits</td>
    <td><input type="checkbox" name="chk0" onclick="toggleCheckboxes(this, 'frmDinnerMenu', 'chk0');"/></td>
</tr>
<tr style="display: none;" id="child-0">
    <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Apple</td>
    <td><input type="checkbox" value="0" name="chk0[0]" onclick="toggleParentCheckboxes(this, 'frmDinnerMenu');"/></td>
</tr>
<tr style="display: none;" id="child-0">
    <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Banana</td>
    <td><input type="checkbox" checked value="0" name="chk0[1]" onclick="toggleParentCheckboxes(this, 'frmDinnerMenu');"/></td>
</tr>
<tr style="display: none;" id="child-0">
    <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Orange</td>
    <td><input type="checkbox" checked value="0" name="chk0[2]" onclick="toggleParentCheckboxes(this, 'frmDinnerMenu');"/></td>
</tr>

<tr><td><img class="parent" id="1" src="arrow_collapsed.gif" title="Click to Expand">Category - Vegetables</td><td><input type="checkbox" name="chk1" onclick="toggleCheckboxes(this, 'frmDinnerMenu', 'chk1');"/></td></tr>
<tr style="display: none;" id=child-1><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Cabbage</td><td><input type="checkbox" checked value="0" name="chk1[]" onclick="toggleParentCheckboxes(this, 'frmDinnerMenu');"/></td></tr>
<tr style="display: none;" id=child-1><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Tomatoes</td><td><input type="checkbox" checked value="0" name="chk1[]" onclick="toggleParentCheckboxes(this, 'frmDinnerMenu');"/></td></tr>
<tr style="display: none;" id=child-1><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Green Peppers</td><td><input type="checkbox" checked value="0" name="chk1[]" onclick="toggleParentCheckboxes(this, 'frmDinnerMenu');"/></td></tr>
<tr><td><img class="parent" id="2" src="arrow_collapsed.gif" title="Click to Expand">Category - Dessert</td><td><input type="checkbox" name="chk2" onclick="toggleCheckboxes(this, 'frmDinnerMenu', 'chk2');"/></td></tr>
<tr style="display: none;" id=child-2><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Ice Cream</td><td><input type="checkbox" checked value="0" name="chk2[]" onclick="toggleParentCheckboxes(this, 'frmDinnerMenu');"/></td></tr>
<tr style="display: none;" id=child-2><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Custard</td><td><input type="checkbox" checked value="0" name="chk2[]" onclick="toggleParentCheckboxes(this, 'frmDinnerMenu');"/></td></tr>
<tr style="display: none;" id=child-2><td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Chocolate Cake</td><td><input type="checkbox" checked value="0" name="chk2[]" onclick="toggleParentCheckboxes(this, 'frmDinnerMenu');"/></td></tr>
</table>
</form>
</body>
</html>

How Can I fix this, so the toggling works and also the post variables are the way I desired..

Thanks

Vincent
This is an entirely different question than the one you initially asked. You should create a new question-submission.
Jonathan Sampson