Thursday, April 9, 2015

HTML: POST array not showing unchecked checkbox

1
2
3
4
<td>
  <input type="checkbox" name="cb[]"/>
  <input type="checkbox" name="cb[]"/>
</td>
  • Solution 1) Explicit index + hidden trick
  • 1
    2
    3
    4
    5
    6
    <td>
      <input type="hidden" name="cb[0]" value=""/>
      <input type="checkbox" name="cb[0]"/>
      <input type="hidden" name="cb[1]" value=""/>
      <input type="checkbox" name="cb[1]"/>
    </td>
  • Solution 2) Explicit index + fill in the blanks (e.g. with PHP)
  • 1
    2
    3
    4
    <td>
      <input type="checkbox" name="cb[0]"/>
      <input type="checkbox" name="cb[1]"/>
    </td>
    $post_array = $_POST['cb'];
    $defaults = array_fill(0, 2, '0');
    $post_array = $post_array + $defaults;