Unable to get Jquery doing calculations without having to physically change value in field

Unable to get Jquery doing calculations without having to physically change value in field

Hello,

I have a few fields, when the quantity is changed, the total is calculated but there is a problem, if I load the page prepopulated from mysql and change the price value without touching the quantity, the total do not change.



Please see the attached picture to understand the problem better.
Imagine that I load this form with pre populated data(update page), if I change dropdownlist 1 and 2, without physically changing the quantity, then the input field 3(total) will not change.

Any idea why please?

Thank you so much!


  1. <script> 
  2. <div class="form-inline well">
  3. <label for="<?php echo "label".++$label_id; ?>">Choose Product*</label>
  4. <div class="form-group" id="totals">

  5. <!--Product name Select-->
  6. <select name="product" class="form-control products" required>
  7. <option><?php echo $product; ?></option>
  8. <?php 
  9. $query_field3 = "SELECT * FROM products ORDER BY name desc";
  10.                         $result_field3 = mysqli_query($connection, $query_field3) or die (mysqli_error());
  11.                         while($row_field3 = mysqli_fetch_array($result_field3)){
  12. $product_name = $row_field3['name'];
  13. $product_price1 =  $row_field3['price1'];
  14. $product_price2 =  $row_field3['price2'];
  15.                         $product_price3 = $row_field3['price3'];
  16. ?>
  17. <option value="<?php echo $product_name; ?>" data-product_price1="<?php echo $product_price1; ?>" data-product_price2="<?php echo $product_price2; ?>" data-product_price3="<?php echo $product_price3; ?>"><?php echo $product_name; ?></option>
  18.                         <?php } ?>
  19.    </select>

  20. <!--Price Select-->
  21.                         <select name="price" class="form-control prices" required>

  22. <option value="<?php echo $price; ?>"><?php echo $price; ?></option>

  23. </select>

  24. <label for="product1_id">Qty</label>
  25. <input name="quantity" value="<?php echo $quantity; ?>" type="text" class="form-control quantity" maxlength="4" size="4">
  26. <label for="total_id">Total Cost(&euro;)</label>
  27. <input name="total" value="<?php echo $cust_order_total; ?>" type="text" class="form-control total" maxlength="4" size="4">
  28.  </div>
  29.  </div>
  30.    
  31. $(function() {
  32.       $('.form-control.products').change(function() {
  33.             var selected = $('option:selected', this);
  34.             var options = '';
  35.             for (var i = 1; i <= 3; i++) {
  36.                   options += '<option>' + selected.data('product_price' + i) + '</option>';
  37.             }
  38.             $('.form-control.prices').html(options);
  39.       });
  40. });
  41. </script>