Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Basic calculation in order form

1. ## 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,

• 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 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>  ```

• ## 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,

• 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...

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•