misslois74
Programmer
im currently working on a page wherein it make use of a table that computes for the sum of values for each row as well as getting the sum for an entire column and here is the code:
its working just fine now the problem is how will i be able to store the sum of those values in the database what could be best way to do it?
i know that most of the code deals with javascript but how will i integrate it with php
Code:
<script type="text/javascript">
function disen(c) {
var otd=c.parentNode;
var otr=otd.parentNode;
for (var j=1;j<otr.cells.length;j++) {
if (!c.checked) {
otr.cells[j].getElementsByTagName("input")[0].value="";
}
otr.cells[j].getElementsByTagName("input")[0].disabled=!c.checked;
}
}
function doit() {
var otbl=document.getElementById("tblid");
var sum, cv, nrow, ncell;
for (var i=1;i<otbl.rows.length-1;i++) {
sum=0;
ncell=otbl.rows[i].cells.length-1;
if (otbl.rows[i].cells[0].getElementsByTagName("input")[0].checked) {
for (var j=1;j<ncell;j++) {
cv=otbl.rows[i].cells[j].getElementsByTagName("input")[0].value;
if (cv.length==0||!/^\s*[+-]?\d*(\.\d*)?\s*$/.test(cv)) {
otbl.rows[i].cells[j].getElementsByTagName("input")[0].value="";
cv=0;
}
sum+=parseFloat(cv);
}
otbl.rows[i].cells[ncell].getElementsByTagName("input")[0].disabled=false;
otbl.rows[i].cells[ncell].getElementsByTagName("input")[0].value=sum;
}
}
nrow=otbl.rows.length-1; //last row
ncell=otbl.rows[nrow].cells.length-1; //last row's last cell
for (var j=1;j<otbl.rows[nrow].cells.length;j++) {
sum=0
for (var i=1;i<otbl.rows.length-1;i++) {
cv=otbl.rows[i].cells[j].getElementsByTagName("input")[0].value;
if (cv.length==0||!/^\s*[+-]?\d*(\.\d*)?\s*$/.test(cv)) {
otbl.rows[i].cells[j].getElementsByTagName("input")[0].value="";
cv=0;
}
sum+=parseFloat(cv);
}
otbl.rows[nrow].cells[j].getElementsByTagName("input")[0].value=sum;
}
}
</script>
</head>
<body>
<table border=0 cellpadding=1 cellspacing=5 id="tblid"> <!-- I add an id to facilitate the referencing. -->
<tr>
<th>Days of Month</th>
<th>Kids</th>
<th>Adults</th>
<th>Volunteers</th>
<th>subtotal</th> <!-- added column -->
</tr>
<?php
for($i=1;$i<=31;$i=$i+1)
{
echo '<tr>
<td>'.$i.' <input type="checkbox" onclick="disen(this)"></td>
<td><input type="text" disabled="disabled" size=5 ></td>
<td><input type="text" disabled="disabled" size=5 ></td>
<td><input type="text" disabled="disabled" size=5 ></td>
<td><input type="text" disabled="disabled" size="5" readonly="readonly" /></td>
</tr>';
}
?>
<tr>
<td><span style="font-weight:bold;"> subtotal:</span></td>
<td><input type="text" size="5" readonly="true" /></td>
<td><input type="text" size="5" readonly="true" /></td>
<td><input type="text" size="5" readonly="true" /></td>
<td><input type="text" size="5" readonly="true" /></td>
</tr>
</table>
<button onclick="doit()">update subtotals</button><br />
its working just fine now the problem is how will i be able to store the sum of those values in the database what could be best way to do it?
i know that most of the code deals with javascript but how will i integrate it with php