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!

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

  • 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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.