How to sum values populated from the database
<p>I have a dynamic table being populated from the database. My code is as below : </p>

<pre><code>&lt;table class="table table-bordered" id="tb"&gt;
  &lt;thead&gt;
    &lt;tr style="BACKGROUND-COLOR: DarkGrey "&gt;
      &lt;th colspan="5" style="text-align:Center;"&gt;Objectives&lt;/th&gt;
    &lt;/tr&gt;
    &lt;tr class="tr-header"&gt;
      &lt;th style="text-align:Center;width:10%;"&gt;ID&lt;/th&gt;
      &lt;th colspan="2"&gt;Targets&lt;/th&gt;
      &lt;th style="text-align:Center;width:1%;" rowspan="2"&gt;Weightage %&lt;/th&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;th&gt; &lt;/th&gt;
      &lt;th&gt; &lt;/th&gt;
      &lt;th style="width:10%;"&gt; Date &lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody
  &lt;?PHP
  $myquery = "select subtotals,Serial_Number,Targets,Weightage, row_number,edit_details
              from Table1 WHERE Serial_Number='$Serial_Number' 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'];
    $Targets = $res['Targets'];
    $Row_Number = $res['row_number'];
    $subtotals = $res['subtotals'];
    ?&gt;
    &lt;tr id="&lt;?php //echo $id  ?&gt;" class="tredit"&gt;
      &lt;td class="edittbl"&gt;
        &lt;b&gt;&lt;input type="text" name="ID[]" class="ASDF form-control" id="Row&lt;?php echo $Row_Number ?&gt;" value="&lt;?php echo $Row_Number; ?&gt;" readonly&gt;&lt;/input&gt;&lt;/b&gt;
      &lt;/td&gt;                                               
      &lt;?PHP
      if ($Objectives == "SubTotal") {
        ?&gt;
        &lt;td class="edittbl" colspan="3"&gt;
          &lt;b&gt;&lt;input type="text" name="Subtotal[]" class=" ASDF form-control" value="&lt;?php echo $Objectives; ?&gt;" style="text-align:center;" readonly &gt;&lt;/input&gt;&lt;/b&gt;
        &lt;/td&gt;   
        &lt;td class="edittbl"&gt;
          &lt;b&gt;&lt;input type="text" name="SubTotal_Weightage[]" class="ASDF form-control" value="&lt;?php echo $Weightage; ?&gt;" readonly&gt;&lt;/input&gt;&lt;/b&gt;
        &lt;/td&gt;                                               
        &lt;?PHP
      } else {
        ?&gt;
        &lt;td class="edittbl"&gt;
          &lt;input type="text" name="Targets[]" class="ASDF form-control" value="&lt;?php echo $Targets; ?&gt;" &gt;&lt;/input&gt;
        &lt;/td&gt; 
        &lt;td class="edittbl"&gt;
          &lt;input type="text" name="Weightage[]" class="ASDF form-control" value="&lt;?php echo $Weightage; ?&gt;"&gt;&lt;/input&gt;
        &lt;/td&gt;
      &lt;?PHP } ?&gt;
      &lt;/tr&gt;                   
    &lt;?PHP } ?&gt;
  &lt;/tbody&gt;
&lt;/table&gt;
</code></pre>

<p>I am trying to do a auto calculate on sub-totals when the values change, such that when the weightage changes, the sub-totals auto calculate and give the new value.</p>

<p><a href="https://i.stack.imgur.com/sdL04.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sdL04.png" alt="Calculation Image"></a></p>

<p>These weightages are dynamic, and the subtotal's donot have a fixed position. How do I ensure that if there's any change in the first four it changes the 1st sub-total and if there's any change in the 2nd and 3rd one it changes the 2nd sub-total? </p>

<p>Would appreciate any suggestion.</p>