/    Sign up×
Community /Pin to ProfileBookmark

Help with function to keep running total of prices

Hi,

I have an order form now with nearly 90 products to be split into several separate categories that will be subtotaled, then all added together at the end. Each product is on a separate line and when the quantity is entered, the price on that line is updated. I would like it so that as each product is updated, the subtotal for that category is automatically updated to reflect the change. The subRetailFood function is my attempt to keep a running total.

Can someone help, please?

[code]

// Function to round to two decimal places

function round (n, d) {
n = n – 0;
d = d || 2;
var f = Math.pow(10, d);
n = Math.round(n * f) / f;
n += Math.pow(10, – (d + 1));
n += ”;
return d == 0 ? n.substring(0, n.indexOf(‘.’)) :
n.substring(0, n.indexOf(‘.’) + d + 1);
}

// Running Subtotal of Food Products Category items 1 thru 22

[COLOR=darkred]var rfood_subtotal = 0;[/COLOR]
function subRetailFood () {
for (i=0; i<f.rPrice.value; i++) {
document.write.rfood_subtotal;
}
}

// Determine line by line retail and wholesale prices for each product after quantity ordered

function determineRWPrices(){
var f = window.document.mProducts; // f for form

// r Price is total Retail Price
// w Price is total Wholesale Price

var qty1 = f.qty1.value; // Quantity of items ordered
f.rPrice1.value = “$” + round(f.drPrice1.value * qty1); // Default retail price times quantity
f.wPrice1.value = “$” + round(f.dwPrice1.value * qty1); // Default wholesale price times quantity

var qty2 = f.qty2.value;
f.rPrice2.value = “$” + round(f.drPrice2.value * qty2);
f.wPrice2.value = “$” + round(f.dwPrice2.value * qty2);

var qty3 = f.qty3.value;
f.rPrice3.value = “$” + round(f.drPrice3.value * qty3);
f.wPrice3.value = “$” + round(f.dwPrice3.value * qty3);

[/code]

and here’s how the form is set up:

[code]

<table width=”100%” cellpadding=”1″ class=”wsproducts”>
<tr>
<th colspan=”8″>FOOD PRODUCTS</th>
</tr>
<tr>
<th width=”4%”>Qty</th>
<th width=”6%”>Stock No.</th>
<th width=”28%”>Product</th>
<th width=”8%”>Retail</th>
<th width=”9%”>Wholesale</th>
<th width=”10%”>Total Retail</th>
<th width=”24%”>Total Wholesale</th>
<th width=”11%”>&nbsp;</th>
</tr>
<tr>
<td><input name=”qty1″ type=”text” size=”3″></td>
<td><input type=”hidden” name=”stock” value=”100″>
100</td>
<td><input type=”hidden” name=”product” value=”Beer Bread”>
Old World Beer Bread</td>
<td><input type=”hidden” name=”drPrice1″ value=”4.99″>
$4.99</td>
<td><input type=”hidden” name=”dwPrice1″ value=”3.24″>
$3.24</td>
<td><input name=”rPrice1″ type=”text” size=”10″></td>
<td><input name=”wPrice1″ type=”text” size=”10″></td>
<td><input type=”button” value=”ADD” onClick=”determineRWPrices(); return false;” name=”button”></td>
</tr>
<tr>
<td><input name=”qty2″ type=”text” id=”qty2″ size=”3″></td>
<td><input type=”hidden” name=”stock” value=”101″>
101</td>
<td><input type=”hidden” name=”product” value=”Cranberry Muffins”>
Cranberry Orange Muffins</td>
<td><input type=”hidden” name=”drPrice2″ value=”5.99″>
$5.99</td>
<td><input type=”hidden” name=”dwPrice2″ value=”3.89″>
$3.89</td>
<td><input name=”rPrice2″ type=”text” id=”rPrice2″ size=”10″></td>
<td><input name=”wPrice2″ type=”text” id=”wPrice2″ size=”10″></td>
<td><input type=”button” value=”ADD” onClick=”determineRWPrices(); return false;” name=”button”></td>
</tr>

<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td colspan=”2″><div align=”right”><strong>SUBTOTAL FOOD
ITEMS:</strong></div></td>
[COLOR=darkred]<td><input name=”rSubFood” type=”text” size=”10″ onFocus=”subRetailFood();”></td>[/COLOR]
<td><input name=”wSubFood” type=”text” size=”10″></td>
<td>&nbsp;</td>
</tr>
[/code]

Any help is very much appreciated. I’m really stuck on this and (obviously) don’t know enough about Javascript and I’m desperate to get this form working.

Thanks.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@Mr_JJul 25.2003 — How many categories is there gping to be?
Copy linkTweet thisAlerts:
@kspauthorJul 26.2003 — Thanks, it's seven but that will actually vary.

Actually, the code I posted earlier has changed so that it's consolidated, so for now I guess this post isn't valid anymore and I'll start a new one if needed.

Thanks again...
×

Success!

Help @ksp spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...