View Full Version : Adding 2 Fields Multiply 3 Fields

04-07-2006, 07:09 PM
Im trying to get 3 of my fields to multiply ex: A+B*C = D
and 2 of my fields to add ex: D+E=F

the form name is "edit" and i have these fields: tax, scost, mcost, lcost, tcost, and taxc. TAXC is a hidden field with a preset value of .095

Heres my javascript:

function total() {
A = document.edit.tax.value
B = document.edit.scost.value
A = Number(A)
B = Number(B)
C = (A + B)
document.edit.tcost.value = C

function tax() {
D = document.edit.mcost.value
E = document.edit.lcost.value
F = document.edit.taxc.value
D = Number(D)
E = Number(E)
F = Number(F)
G = (D + E * F)
document.edit.tax.value = G

Any ideas? Please help.

04-07-2006, 07:53 PM
Im trying to get 3 of my fields to multiply ex: A+B*C = D
and 2 of my fields to add ex: D+E=FCare to describe the problem you're having? You've avoided the common problem of trying to add strings (which will result in concatenation, not arithmetic addition), though there are still some issues.

function total() {First, when working with forms and form controls, it's usually best to pass a reference to the element in question. This is especially true if a function can be reused.

In intrinsic event attributes, the this operator will refer to element. For example, in:

<form action="..." onsubmit="return validate(this);">
the validate function will be called when the form is submitted, and its first argument will be a reference to that form.

A = document.edit.tax.value
B = document.edit.scost.valueThere are three issues, here. One is stylistic.

Variables, including globals, should always be declared, and given as little scope as possible.
In my opinion, and undoubtedly other's, adding form elements and form controls as properties of the document and form objects, respectively, was a bad idea. It's sloppy and leads to naming conflicts. There are already two collections - forms and elements - for that purpose. Use these, instead.
I suggest using descriptive names for your variables. It's also good form to terminate statements with semicolons.

A = Number(A)
B = Number(B)Though the Number constructor function can be used to type-convert values and objects to numbers, the unary plus (+) operator is much simpler and more efficient.

The result looks something like:

function total(form) {
var elements = form.elements,
subtotal = +elements.scost.value,
tax = +elements.tax.value;

elements.tcost.value = subtotal + tax;
I assume that the 's' in 'scost' stands for sub-total (or a synonym).

Note that if values are entered by the user, though presumably these particular values aren't, you should validate the input before acting on it.

The tax function would undergo a similar change.

Any ideas? Please help.We will, as soon as you tell us what the problem is. :)


04-10-2006, 12:12 PM
okay the problem im having the Tax dosen't want to calculate and put the Total in the Tax Field. And once you have the tax the Second script total() is supposed to take the Material Cost(mcost), Labor Cost(lcost), Tax(tax), and the Shipping Cost(scost) adding it together to get the total in which displays in the Total Field. There is one hidden Field called taxc which is the tax rate.

Form Example:

<form action="edit.php" method="post" name="edit">
<input name="mcost" type="text" id="mcost" value="">
<input name="lcost" type="text" id="lcost" value="">
<input name="tax" type="text" id="tax" value="">
<input name="taxc" type="hidden" id="taxc" value=".095">
<input name="scost" type="text" id="scost" value="">
<input name="tcost" type="text" id="tcost" value="">
<img src="_img/Tax.png" border="0" onclick="tax()"> <img src="_img/Total.png" border="0" onclick="total()">

04-10-2006, 08:18 PM
I have changed the way i am wanting it done to just adding all the fields together. The tax field well be manually entered.

04-11-2006, 03:13 PM
even when the code is simple as just adding im getting NaN when i click the total button.

04-11-2006, 05:34 PM
Please post a link to the page on your site that contains the problematic script so we can check it out.


04-12-2006, 12:11 PM
It was for an Intranet PHP Based program and the company has decided not to have the calculation fields. Please disregard this Topic now.