...

View Full Version : Basic calculation in order form



brissa
04-18-2005, 04:45 PM
Guys,

Newbie questions :confused:

I create a simple order form

Product Price Quantity Total
------------------------------------------------------
Product A 1000
Product B 2000
Product C 1000
------------------------------------------------------
Sub Total xxxxxx
Shipping xxxxxx
Total xxxxxx

The code :

<script language="JavaScript">
function GetTotal() {

shipping = 100;
subtotal=subtotal+(product-a.value)*1000;
subtotal=subtotal+(product-b.value)*2000;
subtotal=subtotal+(product-c.value)*1000;
total=subtotal+shipping
}
</script>

I put on onchange=GetTotal() on quantity field, but
nothing happened when i change the quantity.

Since I'm new in js, could anyone help me?
Any suggesstions would be really appreciated.

Rgds,

Kor
04-18-2005, 05:19 PM
usually I don't do someone else's homework but you are too lucky today as i am in very good moods, so here's your code. Fell free to ask me whichever you might not understand.



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/JavaScript">
var p = new Array()
p[0] =['a',1000];
p[1] =['b',2000];
p[2] =['c',1000];
function calculate(f){
var subt =0;
for(var i=0;i<p.length;i++){
subt = subt+(Number(f.elements[p[i][0]].value)*p[i][1]);
}
f.elements['st'].value=subt;
f.elements['to'].value=subt+Number(f.elements['sh'].value)
}
</script>
</head>
<body>
<form>
<input name="a" type="text" onkeyup="calculate(this.form)">Product A * 1000 <br>
<input name="b" type="text" onkeyup="calculate(this.form)">Product B * 2000 <br>
<input name="c" type="text" onkeyup="calculate(this.form)">Product c * 1000 <br><br>
<input name="st" type="text" readonly> Sub Total<br>
<input name="sh" type="text" readonly value="100"> Shipping <br>
<input name="to" type="text" readonly> Total <br>
</form>
</body>
</html>

brissa
04-19-2005, 04:57 PM
:thumbsup: thank you Kor, really piece of cake for an expert like you.
Your script is awesome, easy to follow, especially for newbie like me.
I agree, next time i'll do my best doing my homework by myself before
i post here. Please forgive me since js is not my area of expertise.

Rgds,

Kor
04-19-2005, 05:05 PM
Neh... The ideea is that: at least you tried to code something, you tried to folow a logical route to solve a problem, so that, obviousely, as all people here, I decided to help you. Some other people use sometime to enter here and simply say: I want that, and that and that, without even trying to write the slightest trace of a code line... :D

You're welcome. We are here to help anyone who tries learn/improve coding... :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum