Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-18-2005, 03:45 PM   PM User | #1
brissa
New to the CF scene

 
Join Date: Apr 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
brissa is an unknown quantity at this point
Basic calculation in order form

Guys,

Newbie questions

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,

Last edited by brissa; 04-18-2005 at 03:48 PM..
brissa is offline   Reply With Quote
Old 04-18-2005, 04:19 PM   PM User | #2
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
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.

PHP Code:
<!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 
= 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> 
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Old 04-19-2005, 03:57 PM   PM User | #3
brissa
New to the CF scene

 
Join Date: Apr 2005
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
brissa is an unknown quantity at this point
Thank You

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,
brissa is offline   Reply With Quote
Old 04-19-2005, 04:05 PM   PM User | #4
Kor
Red Devil Mod


 
Kor's Avatar
 
Join Date: Apr 2003
Location: Bucharest, ROMANIA
Posts: 8,478
Thanks: 58
Thanked 379 Times in 375 Posts
Kor has a spectacular aura aboutKor has a spectacular aura about
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...

You're welcome. We are here to help anyone who tries learn/improve coding...
__________________
KOR
Offshore programming
-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
Kor is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 04:59 AM.


Advertisement
Log in to turn off these ads.