I'm having trouble in getting the values of the checked and unchecked checkbox. When the checkbox is checked, it will get the value of that particular checkbox and whenever the checkbox is unchecked, the value of the unchecked checkbox will be 0.
This is my html code:
<table border="0" cellspacing="0" cellpadding="1">
<tbody>
<tr style="height: 40px;">
<td style="width: 240px;"><input type="checkbox" name="checkbox[]" value="5"> Barangay business permit </td>
<td style="width: 240px;"><input type="checkbox" name="checkbox[]" value="10"> Business plate </td>
<td style="width: 240px;"><input type="checkbox" name="checkbox[]" value="20"> Sanitary inspection fee </td>
</tr>
<tr style="height: 40px;">
<td style="width: 240px;"><input type="checkbox" name="checkbox[]" value="30"> Environmental clearance </td>
<td style="width: 240px;"><input type="checkbox" name="checkbox[]" value="40"> Barangay development </td>
<td style="width: 240px;"><input type="checkbox" name="checkbox[]" value="50"> building permit </td>
</tr>
<tr style="height: 40px;">
<td style="width: 240px;"><input type="checkbox" name="checkbox[]" value="60"> Electrical inspection </td>
<td style="width: 240px;"><input type="checkbox" name="checkbox[]" value="70"> Mechanical inspection </td>
<td style="width: 240px;"><input type="checkbox" name="checkbox[]" value="80"> Plumbing inspection </td>
</tr>
</tbody>
</table>
<div class="" style="margin: auto; padding-top:20px;">Total <input type="text" name="total" style="color: #222222;" readonly/>
<input type="submit" name="register" value="Register" style="width: 100px; height:50px; margin:auto; color:#222222;">
</form>
PHP code:
$checkbox=$_POST['checkbox'];
for($i=0; $i<count($checkbox); $i++)
{
$num[$i]=$checkbox[$i];
}
Javascript codes (for the function of getting the values of each checkbox while checking and generating a total in the total field.)
<script type="text/javascript">
$(function(){
var applications = [];
//bind the change event to the checkboxes
$('input[name="checkbox[]"]').change(function(){
var total = 0;
//get value from each selected ckeck box
$('input[name="checkbox[]"]:checked').each(function(){
var tval = $(this).val();
total += parseFloat(tval);
});
//finally display the total with a ₱ sign
$('input[name="total"]').val("₱ " + total);
// handle applications
var application = $.trim($(this)[0].nextSibling.nodeValue); // get the name
var i = applications.indexOf(application);
if(i != -1) {
applications.splice(i, 1); // remove if unchecked
} else {
applications.push(application); // push if checked
}
var temp = applications.join(', ');
$('input[name="applications"]').val(temp);
});
});
</script>
In the actual test, I checked some of the checkbox in the table [0, 1, 0, 0, 1, 1, 0, 0, 0]. Note: 1 represents as checked and 0 represents as unchecked to make it clear.
When I click on submit button and display the values, I got this
[0] => 10
[1] => 40
[2] => 50
But I want to get the values like this
[0] => 0
[1] => 10
[2] => 0
[3] => 0
[4] => 40
[5] => 50
[6] => 0
[7] => 0
[8] => 0
How can I get that values? I need to get each value of the unchecked as 0 & checked values then save each array (in order) to database. I really need a help, I searched it from the net but I didn't get the right solution to my problem. I think my php codes is wrong.
database name: application
table: fees
columns: ID, fee1, fee2, fee3, fee4, fee5, fee6, fee7, fee8, fee9