/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Help with calculating totals

Hello,

I’m pretty new to JavaScript, and am trying to get a simple form working, that will add up some totals.

You can see the page here:
[url]http://redboine.door62.com/membership-form.html[/url]

I based this form of of a couple scripts found [URL=”http://www.mcfedries.com/JavaScript/OrderTotals.asp”]here for the “quantity times price”[/URL] (lines 18-47) and [URL=”http://www.mcfedries.com/JavaScript/AddItUp.asp”]here for the pop-up selection for boat length[/URL] (lines 58-67).

I originally had the total working for the quantities, but now it’s not any more. You’ll notice that I also need to add $500 to every total, calculate a discount based on whether or not a checkbox is selected (lines 49-52), and then after all that, calculate 5% tax and add it to the sub total (line 55).

I’m a little lost along this road, this far into it. Sorry for such a beginner question. I hope I didn’t make too much of a mess of the code for this. Any help would be greatly appreciated! I’m assuming it’s just a syntax error of some kind, because these calculations are not particularly complicated.

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@toicontienFeb 06.2008 — Remember that all form fields store their value as a string. I see several cases where you are trying to perform mathematical calculations directly on the form field value. You always want to pass the form field value to the parseFloat or Number functions to properly convert the string to a number.

Related blog post: A Practical Guide To Numbers in JavaScript -- You may be interested in two functions I wrote: toNumber() and textFieldToNumber().
Copy linkTweet thisAlerts:
@mike62authorFeb 09.2008 — Thanks for the help! I've made a lot of progress. My numbers are now adding properly, and tax is being applied correctly.

http://redboine.door62.com/membership-form2.html

I'm still having two issues when I try to move forward with this file:

http://redboine.door62.com/membership-form3.html

  • 1. I can't seem to get the "April 15" checkbox to apply the 5% discount when selected (lines 59 - 63)


  • 2. I can't seem to get the pop-up to add it's value to the total (lines 50 - 53)


  • It's so close I can taste it! Any ideas of where I'm going wrong with these items?

    Thanks so much for the help!
    Copy linkTweet thisAlerts:
    @JMRKERFeb 09.2008 — Assuming I'm looking at the correct lines:
    [code=php]
    // Get the value of the item that's selected in the BOAT LENGTH list
    item = form.first.selectedIndex;
    firstValue = eval(form.first.options[item].value)
    boat_length = Number(round_decimals(firstValue, 2))


    // Add the $500 from the first two items, and the boat length
    subtotal1 = Number(round_decimals(order_total, 2)) + 500.00 + boat_length

    //Add the discount for early submission, if selected
    if (frm.april15.value == true){
    alert("it's checked!");
    subtotal1 = Number(subtotal1) - (Number(subtotal1)*0.05)
    }
    [/code]

    1. You seem to be using different names for the form. I've see 'form', 'form1', 'frm'.


    2. Probably can avoid using the 'eval(...)' altogether when you get the correct value.

    3. The line to check the checkbox status of 'april15' is incorrect syntax.

    4. I don't see a "calculate" or 'submit' button to display the calcualtions.

    5. I don't get to the 'alert()' because of the previous errors.

    Have you been checking the error console?
    Copy linkTweet thisAlerts:
    @mike62authorFeb 09.2008 — 
    1. You seem to be using different names for the form. I've see 'form', 'form1', 'frm'.


    2. Probably can avoid using the 'eval(...)' altogether when you get the correct value.

    3. The line to check the checkbox status of 'april15' is incorrect syntax.

    4. I don't see a "calculate" or 'submit' button to display the calcualtions.

    5. I don't get to the 'alert()' because of the previous errors.

    Have you been checking the error console?[/QUOTE]


    The submit button is coming... this is a form that I'm integrating into a Joomla site and the plugin provides the submit button. The 'alert()' was just for me to test to see if that 'if' statement was actually working.

    Sorry I'm so new at this, I REALLY appreciate the help! I've got everything working great now!!
    Copy linkTweet thisAlerts:
    @JMRKERFeb 09.2008 — Great...Glad I was able to help.
    ×

    Success!

    Help @mike62 spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.17,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...