www.webdeveloper.com
Results 1 to 4 of 4

Thread: I need someone smarter then me to help with a form that calculates. *see inside*

  1. #1
    Join Date
    Mar 2005
    Location
    Southern VA, USA
    Posts
    3

    Question I need someone smarter then me to help with a form that calculates. *see inside*

    I have a form that I am trying to create. I want it to take the users input and multiply it then at the end I want it to add them all up. I have this form live right now if anyone needs to see it just let me know and I'll post the link for the page. Can anyone tell me what type of scripting I need to use?. I also need it to still validate the form (certain items have to be bought in increments)

    Thxs for any and all help

    Mrs.Mac

  2. #2
    Join Date
    Mar 2005
    Location
    Southern VA, USA
    Posts
    3
    I found this code and my luck it works when on his site but when i put it on my site it is not working go figure... anyone have any suggestions I'm all ears...

    and yes i changed the things he said

    here is where i got the code from
    http://www.mcfedries.com/JavaScript/OrderTotals.asp

    <script language="JavaScript" type="text/javascript">
    <!--

    /* This script is Copyright (c) Paul McFedries and
    Logophilia Limited (http://www.mcfedries.com/).
    Permission is granted to use this script as long as
    this Copyright notice remains in place.*/

    function CalculateTotal(frm) {
    var order_total = 0

    // Run through all the form fields
    for (var i=0; i < frm.elements.length; ++i) {

    // Get the current field
    form_field = frm.elements[i]

    // Get the field's name
    form_name = form_field.name

    // Is it a "product" field?
    if (form_name.substring(0,4) == "PROD") {

    // If so, extract the price from the name
    item_price = parseFloat(form_name.substring(form_name.lastIndexOf("_") + 1))

    // Get the quantity
    item_quantity = parseInt(form_field.value)

    // Update the order total
    if (item_quantity >= 0) {
    order_total += item_quantity * item_price
    }
    }
    }

    // Display the total rounded to two decimal places
    frm.TOTAL.value = round_decimals(order_total, 2)
    }

    function round_decimals(original_number, decimals) {
    var result1 = original_number * Math.pow(10, decimals)
    var result2 = Math.round(result1)
    var result3 = result2 / Math.pow(10, decimals)
    return pad_with_zeros(result3, decimals)
    }

    function pad_with_zeros(rounded_value, decimal_places) {

    // Convert the number to a string
    var value_string = rounded_value.toString()

    // Locate the decimal point
    var decimal_location = value_string.indexOf(".")

    // Is there a decimal point?
    if (decimal_location == -1) {

    // If no, then all decimal places will be padded with 0s
    decimal_part_length = 0

    // If decimal_places is greater than zero, tack on a decimal point
    value_string += decimal_places > 0 ? "." : ""
    }
    else {

    // If yes, then only the extra decimal places will be padded with 0s
    decimal_part_length = value_string.length - decimal_location - 1
    }

    // Calculate the number of decimal places that need to be padded with 0s
    var pad_total = decimal_places - decimal_part_length

    if (pad_total > 0) {

    // Pad the string with 0s
    for (var counter = 1; counter <= pad_total; counter++)
    value_string += "0"
    }
    return value_string
    }

    //-->
    </script>

  3. #3
    Join Date
    Jan 2004
    Location
    Melbourne, Australia
    Posts
    5,298
    You will probably want to use JavaScript.

    When are you calling that function? And are you passing the form to it as parameter? Your form naming conventions will have to conform to the names used in the script. eg It looks for field names called "Product".

    Regards.

  4. #4
    Join Date
    Mar 2005
    Location
    Southern VA, USA
    Posts
    3
    actually I dumped that one b/c it was getting on my nerves..lol.. I know just about nothing about java what i do know is just what I remember tidbits from my VB6 class i took years ago..lol.. I found a script and *knock on wood* it is working the only problem I have but i guess i can deal with is that in the field names I have to leave it as the script shows (b/c i'm not swift enough to figure out how to change it and make the form still work) right now the form names are Item1Total there are 3 different types Item#Price, Item#Total, Item#Quantity for 11 different items. and since the results are being emailed I was wanting to change them so that I would be able to easily identify them in the email .. possible adding the product name or part of the product name to the field name so that I can tell the difference. I hope posting off site links is ok ... it just seems easier if you can see the whole page live rather then bits and pieces. here is where the form is currently

    http://www.hightechfarm.com/paymenttest.html

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles