PDA

View Full Version : Getting an accurate grand total



jay019
Jan 24th, 2007, 07:29 AM
Hi there,

I am probably looking at things the wrong way, hopefully soneone can point me in the right direction...

I have a page that has a total field that will have a value when the page is loaded. I also have a few select options that modify the total via an onchange event. Problem is, when i change a select option twice, I cant work out how to get the correct total happening instead of just adding more to the total. I'll show you what I mean...

The HTML...



<form name="orderform">
<input name="price" type="text" id="price" value="<?php echo $_GET['price']; ?>">

<select name="wine" id="wine" onchange="updateTotalPrice(orderform.wine[orderform.wine.selectedIndex].value,orderform.price.value,orderform.price)">
<option selected value="0">Please make your selection</option>
<option value="25">White $25</option>
<option value="25">Red $25</option>
<option value="35">Sparkling $35</option>
</select>

<select name="chocolates" id="chocolates" onchange="updateTotalPrice(orderform.chocolates[orderform.chocolates.selectedIndex].value,orderform.price.value,orderform.price)">
<option selected value="0">Please make your selection</option>
<option value="15">Small $15</option>
<option value="25">Large $25</option>
</select>

...

</form>



And the Javascript...



function updateTotalPrice(ItemCost,SubTotal,Price) {
GrandTotal = parseFloat(ItemCost)+parseFloat(SubTotal);
Price.value = GrandTotal;
}


Is there a better way that will give me a correct total if a user changes their mind and wants the smaller box of chocolates after selecting the larger one???

Any tips or pointers would be much appreciated.

Thanks

jay019
Jan 24th, 2007, 08:24 AM
Damn I'm blind sometimes.
This is what I did...




The HTML...



<form name="orderform">
<input name="price" type="text" id="price" value="<?php echo $_GET['price']; ?>
<input type="hidden" name="subtotal" value="<?php echo $_GET['price']; ?>"/>
">

<select name="wine" id="wine" onchange="updateTotalPrice()">
<option selected value="0">Please make your selection</option>
<option value="25">White $25</option>
<option value="25">Red $25</option>
<option value="35">Sparkling $35</option>
</select>

<select name="chocolates" id="chocolates" onchange="updateTotalPrice()">
<option selected value="0">Please make your selection</option>
<option value="15">Small $15</option>
<option value="25">Large $25</option>
</select>

...

</form>



And the Javascript...



function updateTotalPrice() {
subtotal = parseFloat(document.orderform.subtotal.value);
wine = parseFloat(document.orderform.wine[document.orderform.wine.selectedIndex].value);
chocs = parseFloat(document.orderform.chocolates[document.orderform.chocolates.selectedIndex].value);

TheGrandTotal = subtotal + wine + chocs;
document.orderform.price.value = TheGrandTotal;
}



Have a laugh, I did.

Kor
Jan 24th, 2007, 09:26 AM
Better use local variables. Avoid globals, if not necessary. And you may shorten the select evaluation. If your select is not a multiple one, it will automatically take the value of the selected option value;


function updateTotalPrice() {
var subtotal = parseFloat(document.orderform.subtotal.value);
var wine = parseFloat(document.orderform.wine.value);
var chocs = parseFloat(document.orderform.chocolates.value);
var TheGrandTotal = subtotal + wine + chocs;
document.orderform.price.value = TheGrandTotal;
}

jay019
Jan 25th, 2007, 08:19 AM
Thanks for the heads up!

glenngv
Jan 25th, 2007, 08:09 PM
And you can store the form reference in a local variable to make it more efficient or better yet, pass the form reference to the function from the form element using this.form.

function updateTotalPrice(frm) {
var subtotal = parseFloat(frm.subtotal.value);
var wine = parseFloat(frm.wine.value);
var chocs = parseFloat(frm.chocolates.value);
var TheGrandTotal = subtotal + wine + chocs;
frm.price.value = TheGrandTotal;
}
...
<select name="wine" id="wine" onchange="updateTotalPrice(this.form)">

jay019
Jan 25th, 2007, 10:03 PM
More top tips, thanks guys!
Your going to make a decent coder out of me eventually
:)