Calculate form with Javascript

mike_o 14 Oct, 2010
Hi

I have found a really nice and simple script to calculate prices very easily. Im not a javascript hacker but I wonder is there is a simple solution to create a SUM for each ROW and then SUM up all to a TOTAL? Right now the script only shows the TOTAL. Shall the javascript then be placed in the FORM CODE and in the second box FROM JAVASCRIPT to work with the system?

thanks for feedback 🙂

The Script
<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, 0)
}

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>


The Form
<form id="form1" name="form1" method="post" action=""><table width="500" border="0">
  <tr>
    <td>Program</td>
    <td>grade</td>
    <td>student</td>
    <td>price</td>
    <td>Antal</td>
    <td>Sum</td>
  </tr>
  <tr>
    <td>Event 1</td>
    <td> </td>
    <td> </td>
    <td>3000</td>
    <td><input type="text" name="PROD_event1_3000" id="event1" onChange="CalculateTotal(this.form)" /></td>
    <td><input type="text" name="sum1" id="sum1" /></td>
  </tr>
  <tr>
    <td>Event 2</td>
    <td> </td>
    <td> </td>
    <td>2000</td>
    <td><input type="text" name="PROD_event2_2000" id="event2" onChange="CalculateTotal(this.form)" /></td>
    <td><input type="text" name="sum2" id="sum2" /></td>
  </tr>
  <tr>
    <td>Event 3</td>
    <td> </td>
    <td> </td>
    <td>1000</td>
    <td>
      <input type="text" name="PROD_event3_1000" id="event3" onChange="CalculateTotal(this.form)" />
    </td>
    <td><input type="text" name="sum3" id="sum3" /></td>
  </tr>
        <td>Total</td>
    <td> </td>
    <td> </td>
    <td> </td>
    <td> </td>
    <td><input type="text" name="TOTAL" id="TOTAL" onFocus="this.form.elements[0].focus()"/></td>
  </tr>
</table>
</form>
GreyHead 16 Oct, 2010
Hi mike_o,

The Form Code (without the <form> and </form> tags goes in the Form HTML box; the JavaScript (without the <script> tags) goes into the Form JavaScript box. You will need to debug and modify the script to get it working with ChronoForms.

And, yes, it is possible to add a script to get row sums and a page total.

Bob

PS Using Form HTML, scripts and CSS from other sources is explained in more detail in Chapter 9 of The ChronoForms Book
This topic is locked and no more replies can be posted.