Skylight_lady Posted November 10, 2011 Share Posted November 10, 2011 Hi Guys, I have a few rows in a code that is coming from the database. I'm am trying out a simple calcutaions that i want to build. However, the last code in column doesn't seem to input any total result but does input the rows data in the last column. I hope this makes sence. What this form does is that i have one table we'll call it table1 with amounts in each row collected from the databse AND another table called table2 which allows you to mess with these amounts while the totals etc will automatically be changed to the right amount via the javascript code. Javascript code in header: <script type="text/javascript" language="JavaScript"> function calcRule(anIndex) { form = document.getElementById('form_'+anIndex); Balance = parseFloat(document.getElementById('balanceResult_'+anIndex).value); Payment = parseFloat(document.getElementById('amt_'+anIndex).value); AmtResult = Payment; document.getElementById('amtResult_'+anIndex).innerHTML = AmtResult.toFixed(2) ; num = parseInt(document.getElementById('num_num').value); i = 0; TotAmtResult = 0; TotSum = 0; TotBal = 0; while (i < num) { TotAmtResult = TotAmtResult + parseFloat(document.getElementById('amtResult_'+i).innerHTML); TotSum = TotSum + (parseFloat(document.getElementById('amt_'+i).value)); TotBal = TotBal + (parseFloat(document.getElementById('balanceResult_'+i).value)); i++; } init_int = parseFloat(document.getElementById('totAmtResult1').value.replace(',', '')); TotalInsuranced = (init_int.toFixed(2) - TotAmtResult.toFixed(2)) + (TotAmtResult.toFixed(2) - init_int.toFixed(2)) + (TotAmtResult.toFixed(2) - init_int.toFixed(2)); document.getElementById('totAmtCost').innerHTML = "€"+TotalInsuranced.toFixed(2); init_int2 = parseFloat(document.getElementById('totSum1').value.replace(',', '')); document.getElementById('totCoPA').innerHTML = "€"+TotSum.toFixed(2); init_int3 = parseFloat(document.getElementById('totBal1').value.replace(',', '')); document.getElementById('totBaPA').innerHTML = "€"+TotBal.toFixed(2); init_int3 = parseFloat(document.getElementById('totAmtCost1').value.replace(',', '')); document.getElementById('totAmtCost').innerHTML = "€"+TotAmtCost.toFixed(2); document.getElementById('totAmtResult').innerHTML = "€"+TotAmtResult.toFixed(2); document.getElementById('totSum').innerHTML = "€"+TotSum.toFixed(2); document.getElementById('totBal').innerHTML = "€"+TotBal.toFixed(2); } </script> The form code and result in html with php to collect the original info from database: <form id="<?php echo "form_" .$i; ?>" name="<?php echo "form_" .$i; ?>" action="" method="post"> <input name="num_num" type="hidden" id="num_num" value="<?php echo $num; ?>" /> <th><input type="hidden" align="right" name="balanceResult_<?php echo $i; ?>" id="balanceResult_<?php echo $i; ?>" value="<?php echo $sum_row; ?>" size="6" onchange="calcRule(<?php echo $i; ?>);" /><?php echo number_format($sum_row,2); ?></th> <td><input type="text" align="right" name="amt_<?php echo $i; ?>" id="amt_<?php echo $i; ?>" value="<?php echo $amounts; ?>" size="6" onchange="calcRule(<?php echo $i; ?>);" /></td> <td><input type="hidden" align="right" name="amtResult_<?php echo $i; ?>" id="amtResult_<?php echo $i; ?>" value="<?php echo $amounts; ?>" size="6" onchange="calcRule(<?php echo $i; ?>);" /><div id="amtResult_<?php echo $i; ?>"><?php if ($AmtResult[$i] != $amounts) { $at = $amounts - $AmtResult[$i]; echo number_format($at,2); } else { echo "0.00"; } ?></div></td> <td><a href="#" title="image"><img src="image.png" alt="Get a Quote" /></a></td> </form> . . . . <form action="#"> <input type="hidden" id="totAmtResult1" name="totAmtResult1" value="<?php echo number_format($TotAmtResult,2); ?>" /> <input type="hidden" id="totAmtCost1" name="totAmtCost1" value="<?php echo number_format($TotAmtCost,2); ?>" /> <input type="hidden" id="totSum1" name="totSum1" value="<?php echo number_format($TotSum,2); ?>" /> <input type="hidden" id="totBal1" name="totBal1" value="<?php echo number_format($TotBal,2); ?>" /> </form> . . . . <td align="left" style="font-style:normal; text-align:left;"><div id="totBaPA"><?php echo number_format($TotBal,2); ?></div></td> <td align="left" style="font-style:normal; text-align:left;"><div id="totCoPA"><?php $savtot = number_format($TotSum,2); echo $savtot; ?></div></td> <td align="left" style="font-style:normal; text-align:left;"><div id="totAmtCost"><?php echo "0.00"; ?></div></td> From the following code: $at = $amounts - $AmtResult[$i]; I am trying to get the original amount from the row of table1 (which it currently shows in table2) and minus it by the new code that was changed by the user. Even if i make the change in table2 then the $at doesn't even change when it should. Finally, no result is shown for the total cost from the following code: <div id="totAmtCost"> I'd really appreciate if someone can help with this. Quote Link to comment https://forums.phpfreaks.com/topic/250867-simple-calculations-with-javascript-and-php-wont-work/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.