table is under loop
<table>
<tr><input class="txt form-control" name="balance[]" type="text" placeholder="Balance" /></tr>
</table>
,and the balance is added from all the tr
total balance is shown on the debtsum. And it is done by jquery
<div class="col-lg-3"> <input class="debtsum form-control " id="debt" type="text" name="totaldebt" placeholder="Total Debt" disabled /> </div>
Query to store data into database :
$totaldebt = mysqli_real_escape_string($connect,$_POST['totaldebt']);//to store total debt in the database
$table_customer_data="insert into
tbl_customer_data(totaldebt) values ('".$totaldebt."');
Jquery script to add all the data of class txt :
<script>
function calculateSum() {
var sum = 0;
$(".txt").each(function () {
//add only if the value is number
if (!isNaN(this.value) && this.value.length != 0) {
sum += parseFloat(this.value);
}
});
$('#debt').val(sum.toFixed(2));
}
$("table").on("keyup", ".txt", function () {
calculateSum();
});
</script>
The jquery function is working properly, and sums all the input value of balance. But when i submit form to store all the value into database it stores nothing.
Thanks in advance, any help is appreciated.