Serialzearray sun totalamt

 To achieve your goal of calculating the total based on multiple products, their quantities, and amounts using serialized array data, here’s how you can do it.


### HTML Structure:

```html

<form id="productForm">

  <div class="product">

    <select class="product-select" name="product1">

      <option value="0">Select Product</option>

      <option value="100">Product A - $100</option>

      <option value="200">Product B - $200</option>

      <option value="300">Product C - $300</option>

    </select>

    <input type="number" class="quantity" name="quantity1" placeholder="Quantity" value="0">

  </div>

  

  <div class="product">

    <select class="product-select" name="product2">

      <option value="0">Select Product</option>

      <option value="100">Product A - $100</option>

      <option value="200">Product B - $200</option>

      <option value="300">Product C - $300</option>

    </select>

    <input type="number" class="quantity" name="quantity2" placeholder="Quantity" value="0">

  </div>


  <!-- Add more products as necessary -->


  <div>Total Amount: <span id="totalAmount">0</span></div>

</form>

```


### JavaScript/jQuery Code:

```javascript

$(document).ready(function() {

  // Function to calculate total amount

  function calculateTotal() {

    // Serialize form data into an array

    var formData = $('#productForm').serializeArray();


    // Initialize total

    var total = 0;


    // Temporary storage for quantity and amount

    var productAmount = 0, quantity = 0;


    // Loop through the serialized array

    $.each(formData, function(i, field) {

      if (field.name.includes('product')) {

        // Capture the product amount

        productAmount = parseFloat(field.value) || 0;

      }

      

      if (field.name.includes('quantity')) {

        // Capture the quantity for the corresponding product

        quantity = parseFloat(field.value) || 0;


        // Calculate product total (amount * quantity)

        total += productAmount * quantity;


        // Reset productAmount for next iteration

        productAmount = 0;

      }

    });


    // Update total amount display

    $('#totalAmount').text(total);

  }


  // Trigger total calculation on change in product or quantity fields

  $(document).on('change keyup', '.product-select, .quantity', function() {

    calculateTotal();

  });

});

```


### Explanation:

1. **Serialize the Form**: The `$('#productForm').serializeArray()` function serializes all form fields into an array of objects, which includes all products and their respective quantities.

   

2. **Loop Through Form Data**: The `$.each()` method iterates over each serialized form field:

   - It checks for `product` and `quantity` fields, captures their values, and calculates the total for each product (multiplying quantity by the product price).

   - The total is updated by adding each product's total to the overall total.


3. **Event Listeners**: The `change` and `keyup` events ensure the total is recalculated every time a product or quantity is updated.


With this approach, the total amount will dynamically update as users select products and adjust the quantities in your form.

Newest
Previous
Next Post »