I have a dynamic table, where Column A is populated dynamically from the database and one column is an input fields for ratings. The code is in the link below :
PHP Code
<?PHP
$myquery=" select ROW_NUMBER,Weightage FROM Appraisal_Objectives WHERE Serial_Number like '%1152' ORDER BY Row_Number asc";
$fetched=sqlsrv_query($conn,$myquery) ;
if( $fetched === false ) { die( print_r( sqlsrv_errors(), true ));}
while($res=sqlsrv_fetch_array($fetched,SQLSRV_FETCH_ASSOC))
{
$Weightage=$res['Weightage'];
$Row_Number=$res['ROW_NUMBER'];
if($Weightage=='==')
{
echo "<tr><td><b>Sub-Total</td></b>";
echo "<td><b><input type='number' class='form-control' value='70'></td></td></b>";
echo "<td><input type='number' name='SubTotals[]' id='SubTotals' class='SubTotals form-control' readonly onchange='calculateGrandTotal(this);'></td></tr>";
}
else
{
echo "<tr><td> </td>";
echo "<td>".$Weightage."</td>";
echo "<td><input type='number' name='Rating[]' id='Rating' class='Rating form-control' onkeypress='return isNumberKey(event)' onChange='calc(this.parentElement.parentElement);calculateSubTotal(this);'></td>";
echo "<td style='display:none'><input type='number' name='max_rating[]' id='max_rating' class='max_rating form-control' onChange='calc(this.parentElement.parentElement));' value='$Weightage' td>";
echo "<td style='display:none'><input type='number' name='Row_Number[]' id='Row_Number' class='Row_Number form-control' value='$Row_Number' td></tr>";
}
}
?>
What I am trying to do :
All the data elements should add up and give the sub-total in the Sub-Total field present on top of the fields. The sub-totals should then add up and and give a grand total. I've attached a photo for better understanding.
My issue :
When I am inputing values instead of going to their respective subtotals, they add up all together. Where have I gone wrong? Appreciate any suggestions..
NB : The fields are dynamic, and can change the row number depending on the database values. Calculation needs to be done depending on the dynamic fields and NOT the static fields that I have provided in the code.