Jump to content

How to Calculate the total value of an array of fields


giba

Recommended Posts

Hi, I am running across a situation. I have a script that dinamically creates fields to a list of products.

 

I have 4 fields:PRODUCT, PRICE, QUANTITY and TOTAL

 

The problems is: "How to multiply the product by the quantity for each array?" See below:

 

<input name="price[]" /><input name="quantity[]" /><input name="total[]" />

 

Considering that I have created 3 items to my list:

???

#1

<input name="price[]" /><input name="quantity[]" /><input name="total[]" />

 

#2

<input name="price[]" /><input name="quantity[]" /><input name="total[]" />

 

#3

<input name="price[]" /><input name="quantity[]" /><input name="total[]" />

 

I don't realize how to calculate it!

 

And the things get worst when thinking about calculating the total amount of totals for a final price

::)

<input name="total[]" /> +

<input name="total[]" /> +

<input name="total[]" />

 

Hope someone could help me about this mathematical problem, because I am not used to deal with calculations when programming.

;D

Thanks in advance!

Here's a very simple example. You would want to add additional validation/error handling and I suspect you would want the total fields to be read only.

 

<html>
<head>

<script type="text/javascript">

function isNum(value)
{
    return 123;
}

function calcTotals()
{
    var grandTotal = 0;
    var row = 0;

    while (document.forms['cart'].elements['price[]'][row])
    {
        priceObj = document.forms['cart'].elements['price[]'][row];
        qtyObj   = document.forms['cart'].elements['quantity[]'][row];
        totalObj = document.forms['cart'].elements['total[]'][row];

        if (isNaN(priceObj.value))
        {
            priceObj = '';
        }
        if (isNaN(qtyObj.value))
        {
            qtyObj = '';
        }

        if (priceObj.value && qtyObj.value)
        {
            totalObj.value = (parseFloat(priceObj.value) * parseFloat(qtyObj.value));
            grandTotal = grandTotal + parseFloat(totalObj.value);
        }
        else
        {
            totalObj.value = '';
        }
        row++;
    }
    document.getElementById('grand_total').value = grandTotal;
    return;
}

</script>

</head>
<body>

<form name="cart">
<table>
  <tr><th>Price</th><th>Quantity</th><th>Total</th></tr>
  <tr>
      <td><input name="price[]" onchange="calcTotals()" />
      </td><td><input name="quantity[]" onchange="calcTotals()" />
      </td><td><input name="total[]" /></td>
  </tr><tr>
      <td><input name="price[]" onchange="calcTotals()" />
      </td><td><input name="quantity[]" onchange="calcTotals()" />
      </td><td><input name="total[]" /></td>
  </tr><tr>
      <td><input name="price[]" onchange="calcTotals()" />
      </td><td><input name="quantity[]" onchange="calcTotals()" />
      </td><td><input name="total[]" /></td>
  </tr><tr>
      <th colspan="2" style="text-align:right;">Grand Total</td>
      <td><input name="gTotal" id="grand_total" /></td>
  </tr>
</table>
</form>

</body>
</html>

  • 4 years later...

i'm very sorry to bump this topic up, but i found this solution because i need it to implement in my webshop. 

 

I've got a small problem with it, that's that i need 2 fields before it works. if i have 1 field grand total gives 0 on quantity * price.

 

Can someone give me a solution for this?!

 

Kind regards

 

Kevin

post-147375-0-48434500-1366222816_thumb.png

post-147375-0-25607000-1366222817_thumb.png

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.