0
votes

I have a form in php that displays checkboxes. These checkboxes are associated with a numerical value populated from mysql. What I'm looking to do is add the values of each checkbox, but only if the box is checked.

The problem I am running into is no matter which boxes I have checked, the value from the first checkbox(es) are returned. For example, if there are 5 total checkboxes and I select the bottom 2, the returned sum is for the 2 top boxes not the bottom boxes. It seems my php code knows boxes are being checked, but just doesn't know which boxes are being checked.

Here is my form code

echo "<input type=\"hidden\" name=\"ID[]\" value=\"".$row['ID']."\" />";
echo "<tr><td>&nbsp;<input type=\"checkbox\" name=\"checked[]\" value=\"Y\"></td>";
echo "<input type=\"hidden\" name=\"amount[]\" value=\"".$row['amount']."\" />";

and here is my post

if('POST' == $_SERVER['REQUEST_METHOD']) {
$amt = 0;
$totamt = 0;
foreach($_POST['ID'] as $i => $id)
{

$id = mysql_real_escape_string($id);
  $checked = mysql_real_escape_string($_POST['checked'][$i]);
  $amt = mysql_real_escape_string($_POST['amount'][$i]);

if ($checked == "Y") {
$totamt = $totamt + $amt;
$amt = 0;
}

}
echo $totamt;
}

Thank you for your help.

3
You probably need JavaScript's onclick or onchange events to change the attributes of your input elements.void
Couple of things to check: is the amount field different for every row (check the HTML output). Check the output of print_r($_POST). What are you seeing there. Maybe there's something happening that you're not seeing.Robbert

3 Answers

2
votes

The only checkboxes that are sent from the form are the ones that are checked, and the array indexes will start from 0 no matter which ones they are. So there's no correspondence between the indexes of the checkboxes and the indexes of the hidden fields. There are a few ways to deal with this.

One way is to put explicit indexes in the checkbox names:

echo "<tr><td>&nbsp;<input type=\"checkbox\" name=\"checked[".$row['ID']."]\" value=\"Y\"></td>";
echo "<input type=\"hidden\" name=\"amount[".$row['ID']."]\" value=\"".$row['amount']."\" />";

Then you can add up:

$totamt += $_POST['amount'][$_POST['checked'][$i]];

Another way is to put the amounts directly in the value of the checkboxes, instead of the useless Y value:

echo "<tr><td>&nbsp;<input type=\"checkbox\" name=\"checked[]\" value=\"".$row['amount']."\" /></td>";

Then you do:

$totamt += $_POST['checked'][$i];

A third way is to put explicit indexes in the names of all the fields, instead of letting PHP assign them when the form is submitted:

echo "<input type=\"hidden\" name=\"ID[".$i."]\" value=\"".$row['ID']."\" />";
echo "<tr><td>&nbsp;<input type=\"checkbox\" name=\"checked[".$i."]\" value=\"Y\"></td>";
echo "<input type=\"hidden\" name=\"amount[".$i."]\" value=\"".$row['amount']."\" />";

where $i is a variable that you increment as you're generating the form. This will make the indexes work the way your form code expects.

0
votes

Looks like your hidden field and the actual checkbox have different names try putting them the same name

<input type='hidden" name='checkbox" value="no" />
<input type="checkbox" name="checkbox" value=$row['ID'] />

That way when you check the $_POST for checkbox name it will either show no or the id and you can determine what was checked

Basically the name of the hidden and checkbox should be the same

0
votes

Another option to the ones already mentioned would to use JavaScript to do your adding and store the result in a hidden input field. You could use the onclick even to run a JS function that would add up the values (and you should definitely set it in the value of the checkbox instead of a hidden field). Then store your total into the hidden input field value. This would get passed in your POST along with the checkboxes. If you wanted to double check the amount, you could sum up the values of the checkboxes & compare to the hidden field.