I'm using a way to calculate the value of this id id="salenag<?php echo $a; ?>" 
multiple values which come from the database using PHP and then I'll add value into the input field the sum of all multiples input fields show in id no sale_nag.
I'm using for this a Javascript method but it did not work;
how it works correctly  its use for point of sale to stock out. I want a solution. Please help me out.
<input type='text' value='0' id='sale_nag'>
  <table>
    <?php
    $a =1;
    while ($pro_row = mysqli_fetch_assoc($select_pro)) {
      $rm =  $pro_row['room'];
      $ra =  $pro_row['rack'];
      $sal =  $pro_row['sale_nag'];
      $ma = $pro_row['marka'];
      $cha = $pro_row['chalan'];
      $pro_query2="SELECT *,SUM(sale_nag) AS snag FROM stockout_details  WHERE  marka='$ma' and chalan='$cha' and room='$rm' and rack='$ra'
      ";
      $select_pro2 = mysqli_query($con, $pro_query2);
      $pro_row2 = mysqli_fetch_assoc($select_pro2);
      if ( $sal - $pro_row2['snag'] > 0) {
        ?>
        <div class="form_grid_2 d-none"  >
          <input name="inv[]" required class=""  value="<?php echo $inv_pro1 ; ?>"   type="text"/>
          <span class="label_intro ">inv #</span>
        </div>
        <div class="form_grid_2 d-none"  >
          <input name="purchi[]" required class=""  value="<?php echo $id ; ?> "   type="text"/>
          <span class="label_intro ">Purchi #</span>
        </div>
        <div class="form_grid_2 d-none"  >
          <input name="invstockin[]" required class="" readonly value="<?php echo $pro_row['stockoutinv'];?>"   type="text"/>
          <span class="label_intro ">Inv#</span>
        </div>
        <div class="form_grid_2"  >
          <input name="marka[]" required class=""  readonly value="<?php echo $ma = $pro_row['marka'];?>"   type="text"/>
          <span class="label_intro ">Marka #</span>
        </div>
        <div class="form_grid_2 "  >
          <input name="chalan[]" required class=" " readonly value="<?php echo $cha = $pro_row['chalan'];?>"   type="text"/>
          <span class="label_intro ">Chalan #</span>
        </div>
        <div class="form_grid_2 "  >
          <?php
          ?>
          <input name="nag[]"  class="" readonly value="<?php
          echo   $sal - $pro_row2['snag'] ;
          ?>"    type="text"/>
          <span class="label_intro ">NAG #</span>
        </div>
        <div class="form_grid_2 " >
          <input name="outnag[]"  id="salenag<?php echo $a; ?>" onfocusout="chachvalue(<?php echo $a; ?>);" class=""  value="0"    type="text"/>
          <span class="label_intro ">Nag stock-out #</span>
        </div>
        <div class="form_grid_2 "  >
          <input name="room[]" required class="" readonly value="<?php echo $pro_row['room'];?>"   type="text"/>
          <span class="label_intro ">Room #</span>
        </div>
        <div class="form_grid_2 "  >
          <input name="rack[]" required class="" readonly  value="<?php echo $pro_row['rack'];?>"   type="text"/>
          <span class="label_intro ">Rack #</span>
        </div>
        <script type="text/javascript">
        function  chachvalue(<?php echo $a;?>){
          var x1 = document.getElementById('salenag<?php echo $a ;?>').value;
          var x2 = document.getElementById('sale_nag').value;
          var result  = x1 + x2 ;
          document.getElementById('sale_nag').value=result;
        }
        </script>
        <?php
  $a++; }  
      }
      ?>
    </table>
 
     
    