...

View Full Version : Script to total up prices on page



dawilis
07-24-2002, 10:10 AM
I have a page that I need the prices to total o the page,
can someone offer a suitable script to do this, My knowledge is fairly limited, so it would need to be pretty self explanetory

http://users.highway1.com.au/~willis/flower/order.htm

any help is greatly apprecated
Daryl

Mrs G
07-24-2002, 06:48 PM
There are a few factors to take into account when requiring something like this.

How many items are to be added.
The layout of your page.
Other unforeseen circumstances.


Here's an example of a script that will do something like what you are after.


<SCRIPT language=JavaScript>
<!--
var total=0;
var item1=10.5;
var item2=21.32;
var item3=33.00;
var item4=49.95;
var item5=51.25;

function calc()
{
if (document.sales2.item1.checked == true){
total += item1
}
if (document.sales2.item2.checked == true){
total += item2
}
if (document.sales2.item3.checked == true){
total += item3
}
if (document.sales2.item4.checked == true){
total += item4
}
if (document.sales2.item5.checked == true){
total += item5
}

document.sales2.subtotal.value=total;
document.sales2.vat.value=(document.sales2.subtotal.value*0.175)
document.sales2.total.value=(document.sales2.subtotal.value*1)+(document.sales2.vat.value*1)
document.sales2.vat.value=roundOff(document.sales2.vat.value,2)
document.sales2.total.value=roundOff(document.sales2.total.value,2)
total = 0;
}

// rounds of the values to decimal places set
function roundOff(value, decimal_place)
{
value = "" + value //convert value to string
decimal_place = parseInt(decimal_place);

var whole = "" + Math.round(value * Math.pow(10, decimal_place));
var decPoint = whole.length - decimal_place;

if(decPoint != 0)
{
result = whole.substring(0, decPoint);
result += ".";
result += whole.substring(decPoint, whole.length);
}
else
{
result = whole;
}
return result;
}
-->
</script>

Below is the layout for the example above

<FORM name=sales2>

<table border=0><tr><td>
Item One
</td><td width=30%>
<INPUT type=checkbox value=100 name=item1 onclick=calc() > 10.50
</td><td align=right>
SubTotal <input type=text name=subtotal size=4>
</td></tr>

<tr><td >
Item Two
</td><td>
<INPUT type=checkbox value=200 name=item2 onclick=calc() > 21.32
</td><td></td></tr>

<tr><td>
Item Three
</td><td>
<INPUT type=checkbox value=300 name=item3 onclick=calc() > 33.00
</td><td align=right>
Vat <input type=text name=vat size=4>
</td></tr>

<tr><td>
Item Four
</td><td>
<INPUT type=checkbox value=400 name=item4 onclick=calc() > 49.95
</td><td></td></tr>

<tr><td>
Item Five
</td><td>
<INPUT type=checkbox value=500 name=item5 onclick=calc() > 51.25
</td><td align=right>
Total <input type=text name=total size=4>
</td></tr>

<tr><td colspan=3 align=center>
<INPUT type=reset value=reset name=reset>
</td></tr></table>

</form>

There are two other totally different examples on my site.

dawilis
07-25-2002, 12:52 AM
Hi Mrs G thanks for that I have something similar that I could not configure for drop down list menus, can you suggest a workaround for dropdown menus
also whats the URL for yur site.
Daryl

Mrs G
07-26-2002, 11:27 PM
See if this helps you out


<SCRIPT language=JavaScript>
<!--
total=0

function calc(n){
total += eval(n)//*1
document.f1.sum.value=total
document.f1.sum.value=roundOff(document.f1.sum.value,2)
}

// rounds of the values to decimal places set
function roundOff(value, decimal_place)
{
value = "" + value //convert value to string
decimal_place = parseInt(decimal_place);

var whole = "" + Math.round(value * Math.pow(10, decimal_place));
var decPoint = whole.length - decimal_place;

if(decPoint != 0)
{
result = whole.substring(0, decPoint);
result += ".";
result += whole.substring(decPoint, whole.length);
}
else
{
result = whole;
}
return result;
}
-->
</script>

<form name=f1>
<select onChange="if(options[selectedIndex].value)calc(options[selectedIndex].value)" size="1">
<option value=none>Price List</option>
<option value="10.35">Price = 10.35</option>
<option value="20.56">Price = 20.56</option>
<option value="30.88">Price = 30.88</option>
<option value="40.23">Price = 40.23</option>
<option value="50.54">Price = 50.54</option>

<P><input name="sum" type="text" value="0" size="4">
<input type="reset" onclick="total=0">
</select>
</form>

dawilis
07-27-2002, 12:38 AM
Thanks you so much for all your troubles.
I decided to drop the list menus and go with checkboxes, it turned out ok.
thanks again

Mrs G
07-27-2002, 09:26 AM
Glad to be of help :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum