use checkboxes to select multiple array members

The sample code here shows how to use a set of checkboxes to select multiple members of an array. In other words you have an array of elements. You want the user to select some of them so that a subset will result.

$mntItem (or $_SESSION['PAI_originalChoices']) is the array.

$_POST['mId'] holds the submitted data.

$_SESSION['PAI_mIds'] is the subset.

Each element of $mntItem is an array. This latter array’s zero indexed element’s value is the id. And its one indexed element is the label. The checkboxes are created with the following function call:

$chkBoxes = chkBoxes($mntItem);

Here is the code for that function:

function chkBoxes($mntItem_in) {
  if (!isset($mntItem_in) OR !is_array($mntItem_in) OR sizeof($mntItem_in) < 1) {
    die("Function failed to create check boxes because no array was passed.");

  $chkB_str = "\n<div>\n   <p><b>Which maintenance items?</b></p>\n";

  Here is the loop that builds the main body of the check boxes.
  $i = 0;
  while ($array_cell = each($mntItem_in))
    $id = $array_cell['value'][0];
    $label = $array_cell['value'][1];
    $chkB_str .=
    "   <div>\n" .
    "      <input type=\"checkbox\" name=\"mId[$i][0]\" id=\"$i\" value=\"$id\"/>\n" .
    "      <label for=\"$i\">$label</label>\n" .
    "   </div>\n" .
    "   <div>\n" .
    "      <input type=\"hidden\" name=\"mId[$i][1]\" value=\"$label\">" .
    "   </div>\n";
    $i += 1;

  $chkB_str .= "</div>\n\n";
  return $chkB_str;

From this code you can see what $_POST['mId'] will look like. It is an array whose elements are also arrays. These latter arrays also have 0 and 1 indexed elements. They correspond to id and label just as in $mntItem. However, the 0 element will only be set if the user made that selection; whereas the 1 element will always be assigned because it is a hidden variable.

Here is the function call which produces the final result:

$_SESSION['PAI_mIds'] = sanitize($_POST['mId']);

CAUTION: I’m leaving a lot of important code out!

Here is the code for sanitize():

function sanitize($array_in) {
  if (!isset($array_in) OR !is_array($array_in) OR sizeof($array_in) < 1) {
    die("Script died at sanitize (990877999). -Programmer.");
  } else {
    $new_array = array();
    $count = 0;
    while ($array_cell = each($array_in))
      if (isset($array_cell['value'][0])) {
        $id= $array_cell['value'][0];
        $label = $array_cell['value'][1];
        $new_array[$count][0] = $id;
        $new_array[$count][1] = $label;

  return $new_array;

About samehramzylabib

See About on
This entry was posted in Coding, PHP Form Fields and tagged , , , , . Bookmark the permalink.


Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s