• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

calculating form fields

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm a rookie at this form calculation stuff so be kind....
I've found information on the site about adding fields but this one has an additional step. I'm trying to do an order form that calculates the totals at the end of the form.
Basically it should do this
quantity of item 1 *30 = total for item 1
quantity of item 2 *35 = total for item 2
total = total for item 1 + total for item 2
I've got everything working except for the total. If I put in a quantity of 1 for item 1 and 1 for item 2 I don't get the correct total.
Below is my code, can anyone help me figure out what I did wrong? I'm over my head here....Thanks!!!

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>temp</title>
<script language="javascript">
function calculate(form)
{
var item1 = form.item1.value;
var item2 = form.item2.value;
var item3 = form.item3.value;
var item4 = form.item4.value;
var total = form.total.value;
form.item2.value = (parseInt(item1) * 30);
form.item4.value = (parseInt(item3) * 35);
form.total.value = (item2 + item4);
}
</script>
</head>
<body>
<form method="POST" action="--WEBBOT-SELF--">
<table border="1" cellspacing="1" width="757">
<tr>
<td width="224" align="center"><strong>Quantity</strong></td>
<td width="39" align="center"><strong>price</strong></td>
<td width="476" align="center"><strong>total</strong></td>
</tr>
<tr>
<td width="224">item1<input type="text" name="item1" size="20"
onchange="calculate(this.form)" value="0"></td>
<td width="39">$30</td>
<td width="476">total for item1 orders<input type="text" name="item2" size="20"></td>
</tr>
<tr>
<td width="224">item2<input type="text" name="item3" size="20"
onchange="calculate(this.form)" value="0"> </td>
<td width="39">$35</td>
<td width="476">total for item 2 orders<input type="text" name="item4" size="20"></td>
</tr>
<tr>
<td width="224"> </td>
<td width="39">total</td>
<td width="476"> <input type="text" name="total" size="20"></td>
</tr>
</table>
<p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset"
name="B2"></p>
</form>
</body>
</html>
 
Ranch Hand
Posts: 218
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Try this!
replace
form.total.value = (item2 + item4);
to
form.total.value = (parseInt(form.item2.value) + parseInt(form.item4.value));
-----------------
 
Scott Christy
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sainudheen Mydeen - Thanks so Much!! That worked perfectly! Oone of these days I'm going to have to actually learn what the heck I'm doing with this javascript stuff. Right now I know just enough to get myself in trouble.
Ok, now that I have the calculations working, I'd like to take it one step further...I'd like to have a sales tax field that would calculate 5% sales tax on the total IF a checkbox is marked stating that they are a resident of a given State. I can handle the calculation part of it, how do I make it perform the calculation only if a checkbox is clicked?
 
Scott Christy
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, better yet, I'd like it to base the decision to calculate sales tax based on what state they've selected from a pull down menu elsewhere in my form. How would I check to see if the state is "WI" then add 5.5% sales tax, otherwise leave the tax at 0%.
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moving to HTML/JavaScript
 
Blueberry pie is best when it is firm and you can hold in your hand. Smell it. And smell this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic