bogie
asked on
Javascript Order Calculator
I'm looking for a JavaScript order form calculator that will automatically put in the price of an item if only the item is chosen from a drop down box. Then the only other variable would be the qty. This would create the total for that line item in an order. Each line could be another item
selected from another drop down box. All items would then go to a sub-total, tax, shipping and grand total. I saw one once like this where the item designated the price instead of a fixed price per line - just can't remember where.
Thanks in advance.
--
Herb Bogart
hbogie@flash.net
selected from another drop down box. All items would then go to a sub-total, tax, shipping and grand total. I saw one once like this where the item designated the price instead of a fixed price per line - just can't remember where.
Thanks in advance.
--
Herb Bogart
hbogie@flash.net
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Adjusted points to 250
it a little bit easier to change number of lines in form,
And you need to make changes (add/remove items)
in only one place.
<html>
<head>
<script>
// number of lines you want to have;
var nl=3;
function initArray()
{this.length = 0;}
prices =new initArray();
products=new initArray();
function addElement(pname,pprice)
{oldlength=products.length
prices.length++;
products.length++;
products[oldlength]=pname;
prices[oldlength]=pprice;
}
//-----------------------
// add/remove items here!
//-----------------------
addElement("--------select
addElement("itemname1",1.9
addElement("itemname2",2.9
addElement("itemname3",3.0
addElement("itemname4",4.0
//------------------------
// you can change change taxes and shipment rules here
// --------------------------
function taxesandshipment(totalsumm
{return totalsumm*1.05+itemsnumber
}
function calc()
{var summ=0;
var totalqty=0;
for(i=0;i<nl;i++)
{var qty;
eval('qty=document.forms[0
for(j=0;j<qty.length;j++)
{if((qty.charAt(j)<"0") || (qty.charAt(j)>"9"))
{qty="0";
eval('document.forms[0].q'
alert("Invalid Qty in Line:"+i);
break;
}
}
var inx;
eval('inx=document.forms[0
var p=prices[inx];
var t=qty*p;
if(p!=0) totalqty+=qty;
eval('document.forms[0].t'
summ+=t;
}
document.forms[0].total.va
tfinal=taxesandshipment(su
document.forms[0].tfinal.v
}
function setform()
{var s ='<FORM onSubmit="return false">';
document.writeln(s);
for(i=0;i<nl;i++)
{s='<SELECT name="line'+i+'" onChange="calc()">';
document.writeln(s);
for(j=0;j<products.length;
{s ='<option value="'+prices[j]+'">'+pr
document.writeln(s);
}
s ='</select>';
s +=' Qty:<INPUT name="q'+i+'" TYPE="TEXT" onChange="calc()" size=12 value="0">'
s +=' $:<INPUT name="t'+i+'" TYPE="TEXT" onChange="calc()" size=12 value="0"><br>'
document.writeln(s);
}
s='<input type="submit" onClick="calc(); return false;" value="Calculate">';
document.writeln(s);
s ='<br>total:<br><input type="text" name="total" size=12 value="" onChange="calc()">'
s+='<br>including 5% tax and $0.30 shipping and handling per item:<br><input type="text" name="tfinal" size=12 value="" onChange="calc()">'
s+='</form>';
document.writeln(s);
for(i=0;i<nl;i++) eval('document.forms[0].li
}
</script>
</head>
<body>
<script>
setform();
</script>
</body>
</html>