Jump to content


Photo

Javascript math's help


  • Please log in to reply
5 replies to this topic

#1 ianhaney

ianhaney

    Advanced Member

  • Members
  • PipPipPip
  • 81 posts

Posted 02 February 2013 - 07:56 AM

Hi

I have the following coding in my website that add's or remove's prices to a quote page and works perfect but I need to insert another input field that takes the order total and divides it by 2

Just wondering how I do that in this coding

Below is the HTML

<form name="checkbox">
    
    One Page<input type="checkbox" name="PROD_CH_70.00" value="70" onchange="CalculateTotal(this.form)">
    <br>
    Two Pages<input type="checkbox" name="PROD_CH_140.00" value="140" onchange="CalculateTotal(this.form)">
    <br>
    Three Pages(Bronze Package)<input type="checkbox" name="PROD_CH_179.00" value="179" onchange="CalculateTotal(this.form)">
    <br>
    Six Pages(Silver Package)<input type="checkbox" name="PROD_CH_339.00" value="339" onchange="CalculateTotal(this.form)">
    <br>
    Ten Pages(Gold Package)<input type="checkbox" name="PROD_CH_549.00" value="549" onchange="CalculateTotal(this.form)">
    <br>
    New Business Start up<input type="checkbox" name="PROD_CH_289.00" value="289" onchange="CalculateTotal(this.form)">
    <br>
    Ecommerce Package<input type="checkbox" name="PROD_CH_449.00" value="449" onchange="CalculateTotal(this.form)">
    <br>
    SEO on all pages<input type="checkbox" name="PROD_CH_30.00" value="449" onchange="CalculateTotal(this.form)">
    <br>
    Logo Design<input type="checkbox" name="PROD_CH_25.00" value="25" onchange="CalculateTotal(this.form)">
    
 <!--<input type="text" name="PROD_DC_15" size="10" maxlength="3" onchange="CalculateTotal(this.form)">
<input type="text" name="PROD_CC_20" size="10" maxlength="3" onchange="CalculateTotal(this.form)">-->
<br><br>
<span style="color: #FFFFFF;">&#xA3;<input type="text" name="TOTAL" size="10" onfocus="this.form.elements[0].focus()
">  
</span>
</form>

Below is the javascript coding

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
            if(form_field.type == 'checkbox') {
                item_quantity = form_field.checked;
            } else {
                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
    }


#2 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 02 February 2013 - 08:29 AM

What have you tried?
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#3 ianhaney

ianhaney

    Advanced Member

  • Members
  • PipPipPip
  • 81 posts

Posted 02 February 2013 - 08:30 AM

Nothing yet to be honest as was trying to work out where to put a / etc in the javascript but could not work it out

#4 Jessica

Jessica

    This is not my name.

  • Gurus
  • 8,982 posts
  • LocationDallas, TX
  • Age:26

Posted 02 February 2013 - 08:45 AM

Well try something and then we can help. You need to make an effort
My goal in replying to posts is to help you become a better programmer, including learning how to debug your own code and research problems. For that reason, rather than posting the solution, I reply with tips and hints on how to find the solution yourself. See below for useful links when you get stuck.

How to Get Good Help: How to Ask Questions | Don't be a help vampire
Debugging Your Code: Debugging your SQL | What does a php function do? | What does a term mean? | Don't see any errors?
Things You Should Do: Normalize Your Data | use print_r() or var_dump()
Lulz: "Functions should not have side effects." - trq

Please take a look at my new PHP/Web Dev blog: The Web Mason - Thanks!!

#5 ianhaney

ianhaney

    Advanced Member

  • Members
  • PipPipPip
  • 81 posts

Posted 02 February 2013 - 09:09 AM

I have had a go and did manage to get the order total to divide by two by adding / 2 on the end of the following

order_total += item_quantity * item_price

So took that out and then inserted the new input field and called it DEPOSIT in the name part

and also added the following in


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

Then tried to do the following


if (item_quantity >= 0) {
order_total += item_quantity * item_price;
else {
order_total += item_quantity * item_price / 2;
}

but that didn't work as that just made both input fields do nothing so took it out again and at the mo it displays the order total in both input fields

see the following link

http://www.irhwebsit...o.uk/quote.html

#6 ianhaney

ianhaney

    Advanced Member

  • Members
  • PipPipPip
  • 81 posts

Posted 02 February 2013 - 09:37 AM

Solved it

I can paste the coding if need be




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Cheap Linux VPS from $5
SSD Storage, 30 day Guarantee
1 TB of BW, 100% Network Uptime

AlphaBit.com