-1

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>
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • **Warning:** You are wide open to [SQL Injections](https://stackoverflow.com/a/60496/1839439) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Sep 10 '20 at 13:38

1 Answers1

0

Since your question is tagged jQuery, I'm assuming you can use it.

First, change your function to this:

function chachvalue(element) { 
    
    // get value of input that unfocused
    var x1 = parseInt(element.val()); 
    
    // select #sale_nag element
    var sale_nag = $('#sale_nag');

    // set value of sale_nag to sale_nag + x1
    sale_nag.val(parseInt(sale_nag.val()) + x1);

}

Now, create a jQuery handler for your inputs

$('.outnag_input').on('focusout', function() {
    chachvalue($(this));
});

Now your input element can be simplified to this

<input name="outnag[]" class="outnag_input" value="0" type="text"/>

You should also move the chachvalue() function AND the focusout handler to outside of the PHP loop.

GrumpyCrouton
  • 8,486
  • 7
  • 32
  • 71