Results 1 to 7 of 7

Thread: Adding 2 Fields Multiply 3 Fields

  1. #1
    Join Date
    Apr 2006
    Posts
    190
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default Adding 2 Fields Multiply 3 Fields

    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:

    Code:
    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.

  2. #2
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by NXArmada
    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
    Care 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:

    HTML Code:
    <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.value
    There are three issues, here. One is stylistic.

    1. Variables, including globals, should always be declared, and given as little scope as possible.
    2. 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.
    3. 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:

    Code:
    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.

    Mike

  3. #3
    Join Date
    Apr 2006
    Posts
    190
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default

    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:
    Code:
    <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()">
    </form>

  4. #4
    Join Date
    Apr 2006
    Posts
    190
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default

    I have changed the way i am wanting it done to just adding all the fields together. The tax field well be manually entered.

  5. #5
    Join Date
    Apr 2006
    Posts
    190
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default

    even when the code is simple as just adding im getting NaN when i click the total button.

  6. #6
    Join Date
    Dec 2004
    Location
    UK
    Posts
    2,358
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Please post a link to the page on your site that contains the problematic script so we can check it out.


    Mike

  7. #7
    Join Date
    Apr 2006
    Posts
    190
    Thanks
    3
    Thanked 7 Times in 7 Posts

    Default

    It was for an Intranet PHP Based program and the company has decided not to have the calculation fields. Please disregard this Topic now.

Bookmarks

Posting Permissions

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