I require the values of unchecked checkboxes to be '0' so I can determine which values have been checked and for all values (checked + unchecked) to be posted ($_POST['chk]). I am using a for loop to create the checkboxes and I am generating a hidden field with a value of '0' (as has been suggested elsewhere on stackoverflow).
There are 4 checkboxes ((count($result)-1) = 4) and when checkbox 1 & 3 are checked while checkbox 2 & 4 are unchecked, the $_POST['chk'] array ends up like this:
Array
(
[chk] => Array
(
[0] => 0
[1] => 180.00
[2] => 0
[3] => 0
[4] => 100.00
[5] => 0
)
I want it to look like this:
Array
(
[chk] => Array
(
[0] => 180.00
[1] => 0
[2] => 100.00
[3] => 0
)
What am I doing wrong? Is this even possible using a for loop and the hidden checkbox fields?
Javascript:
<script type="text/javascript">
function calculate() {
var el, i = 0;
var subtotal = 0;
while(el = document.getElementsByName("chk[]")[i++]) {
if(el.checked) { subtotal = subtotal + Number(el.value);}
}
var node = document.getElementById("subtotal");
node.innerHTML = "$" + subtotal + ".00";
var node = document.getElementById("total");
node.innerHTML = "$" + (subtotal*<?=$no_nights?>) + ".00";
}
</script>
HTML/PHP:
<form id="booking_step2" name="booking_step2" method="POST" action="index.php?p=bookings?s=3">
<? for ($x=0; $x<=(count($result)-1); $x++) { ?>
<input type="hidden" name="chk[]" value="0">
<input type="checkbox" name="chk[]" value="<?=$result[$x]['r_rate'];?>" onclick="calculate()">
<? } ?>