Checkbox input to php script

I am inputing data from an html table using a php script into a mysql database. As part of the design, I want the user to be able to add a new row to the table and am planning to do this using an AJAX function. the table will have a number of columns containing checkboxes to indicate information to be associated with the name of a person indicated in an earlier column. I need to associate that name with which checkboxes are checked or not checked in that row. This information will all be store in various MYSQL tables. Here are several rows of the code generated by php.

<tr>
<input type="hidden" name="id_stakeholder[]" value="74"><td>&nbsp;&nbsp;&nbsp;</td><td><input type="text" name="stakeholder[]" value="Lillisky" style="width:100%" ></td><td><input type="text" name="phone[]" value="510-171-1706" style="width:100%" ></td>
<td align="center" ><input type="checkbox" name="dem_data_a[]" value="Dem_data_checked" ></td>
<td align="center" ><input type="checkbox" name="dem_data_b[]" value="Dem_data_checked" ></td>
<td align="center" ><input type="checkbox" name="dem_data_c[]" value="Dem_data_checked" ></td>
<td align="center" ><input type="checkbox" name="dem_data_d[]" value="Dem_data_checked" ></td>
<td align="center" ><input type="checkbox" name="dem_data_e[]" value="Dem_data_checked" ></td>
<td align="center" ><input type="checkbox" name="dem_data_f[]" value="Dem_data_checked" ></td>
</tr>
<tr>
<input type="hidden" name="id_stakeholder[]" value="75"><td>&nbsp;&nbsp;&nbsp;</td><td><input type="text" name="stakeholder[]" value="Sam" style="width:100%" ></td><td><input type="text" name="phone[]" value="510-171-1706" style="width:100%" ></td>
<td align="center" ><input type="checkbox" name="dem_data_a[]" value="Dem_data_checked" ></td>
<td align="center" ><input type="checkbox" name="dem_data_b[]" value="Dem_data_checked" ></td>
<td align="center" ><input type="checkbox" name="dem_data_c[]" value="Dem_data_checked" ></td>
<td align="center" ><input type="checkbox" name="dem_data_d[]" value="Dem_data_checked" ></td>
<td align="center" ><input type="checkbox" name="dem_data_e[]" value="Dem_data_checked" ></td>
<td align="center" ><input type="checkbox" name="dem_data_f[]" value="Dem_data_checked" ></td>
</tr>
<tr>
<input type="hidden" name="id_stakeholder[]" value="76"><td>&nbsp;&nbsp;&nbsp;</td><td><input type="text" name="stakeholder[]" value="Frank" style="width:100%" ></td><td><input type="text" name="phone[]" value="510-171-1706" style="width:100%" ></td>
<td align="center" ><input type="checkbox" name="dem_data_a[]" value="Dem_data_checked" ></td>
<td align="center" ><input type="checkbox" name="dem_data_b[]" value="Dem_data_checked" ></td>
<td align="center" ><input type="checkbox" name="dem_data_c[]" value="Dem_data_checked" ></td>
<td align="center" ><input type="checkbox" name="dem_data_d[]" value="Dem_data_checked" ></td>
<td align="center" ><input type="checkbox" name="dem_data_e[]" value="Dem_data_checked" ></td>
<td align="center" ><input type="checkbox" name="dem_data_f[]" value="Dem_data_checked" ></td>
</tr>

The problem I am having is that an <input> tag with the name set to an array (e.g. stakeholder) sends an array element to $_POST for every input tag, whether it has a value or not. However, the checkbox tag only sends an input when it is checked. Thus, in the above code, if the checkbox named dem_data_a is not checked in the first row but is checked in the second row, the it will still be assigned a ‘0’ key thus: dem_data_a[0]. this makes it impossible to effectively associate the checked and unchecked boxes in a row with the stakeholder values in that row (unless every checkbox is checked). Thus in the case cited above, $_POST[stakeholder][0] (from the first row) will be associated with dem_data_a[0] in the second row. The wrong data will be associated with it.

I could use an index variable to assign keys to the checkboxes. This way I could keep the $_POST input data associated correctly.

The problem arises when I use ajax to allow the user to add a row. Unless I refresh the entire page and add the row in the php script, I have no way to assign a “hard” key in the appropriate sequence. If $_POST simply sent every checkbox checked or unchecked It would work fine. But, alas this is not the case.

It is interesting that $_POST does simply send the next checked box with the next numeric key, even if it is the 40th instance of a checkbox tag with a given array name.

Any ideas how I can deal with this aside from assigning keys to the checkbox variables?

Thanks,

–Kenoli

Hi,

When I ran this is the out put I got:


array(4) {
  ["id_stakeholder"]=>
  array(3) {
    [0]=>
    string(2) "74"
    [1]=>
    string(2) "75"
    [2]=>
    string(2) "76"
  }
  ["stakeholder"]=>
  array(3) {
    [0]=>
    string(8) "Lillisky"
    [1]=>
    string(3) "Sam"
    [2]=>
    string(0) ""
  }
  ["phone"]=>
  array(3) {
    [0]=>
    string(12) "510-171-1706"
    [1]=>
    string(12) "510-171-1706"
    [2]=>
    string(0) ""
  }
  ["submit"]=>
  string(6) "Submit"
}

If you notice, some of the stakeholder is empty. Is that what you were talking about?

If it is, you can easily fix it by using a simple if condition.

Example:


if (isset($_POST['submit'])) {
    $clean = array();

    foreach ($_POST['stakeholder'] as $person) {
        if ($person !== null) {
            $clean[] = $person;
        }
    }

    var_dump($clean);

    /*
    array(2) {
    [0]=>
    string(8) "Lillisky"
    [1]=>
    string(3) "Sam"
    }
    */
}

That should also fix your ajax issue.

No. The stakeholder array works as I want it returning an empty value when there is no value entered. That is not the problem.

My problem is with the checkbox arrays which return nothing for checkboxes not checked. The problem here is that the checked boxes are returned with numeric keys that don’t leave gaps for the unchecked boxes and this make it impossible to associate them with the <input> arrays.

I’m sure this is something built into html. What I am looking for is a workaround so that in each row, I can access information for each stakeholder that indicates which check boxes in that row were checked.

And, I need something that doesn’t involve assigning array keys.

Thanks,

–Kenoli

Put a hidden field in front of every checkbox giving the hidden field the same name as the checkbox and the value you want used if the checkbox isn’t checked. Then you’ll get the checkbox value passed when its checked and the hidden field value passed when it isn’t checked. You will have to fully qualify each name though with its position in the array and not just use .

Stephen – Clever idea. I think it will work fine if there is one row. I can’t figure out how to make it work when I have a number of rows to update. I tried using array names in various combinations and can’t get it to work correctly. Can’t see how to input multiple rows without array names.

–Kenoli