Calculate a sum from a form

9 posts by 2 authors in: Forums > CMS Builder
Last Post: June 26, 2016   (RSS)

By andreasml - June 21, 2016

Hi

I have created a section editor which contains 25 fields. Each field is in list format. For each option of the list in each field, i give a certain score, ranging from 1 to 7. I would like at the end of the section editor to create a text field which will contain the sum of the scores from the 25 list-fields. 

Any advise on how to do this?

Thanks

Andreas ML

Calculate a sum from a form (update)

By andreasml - June 22, 2016 - edited: June 23, 2016

Update to the previous message.

I would like the value of a text field to be the sum of some fields e.g. field_1, field_2, field_3, all from the same row.

Can i give something like the following code as the default value of the text field?

SUM(field_1 + field_2 + field_3)

Kind regards

Andreas ML

Calculate a sum from a form (update)

By gregThomas - June 23, 2016

Hi Andreas,

You could do this using javascript. If you create a file called custom.js in the base of your CMS Builder installation it will be automatically included by CMS Builder. You could then use jQuery to detect all of the selected values in the fields, total them up, and add them to your total column.

Thanks,

Greg

Greg Thomas







PHP Programmer - interactivetools.com

Calculate a sum from a form (update)

By andreasml - June 23, 2016

Thank  you Greg

It sounds all greek to me!!. Could you be a little more specific? For example, what the custom.js file will be like? 

Thanks

Andreas

Calculate a sum from a form (update)

By gregThomas - June 24, 2016

Hey Andreas, 

The custom.js file will need to look something like this:

$(document).ready(function(){
  $("input[name=field_1], input[name=field_2], input[name=field_3], input[name=field_4]").change(function(){
    value1 = parseFloat($('input[name=field_1]').val()); 
    value2 = parseFloat($('input[name=field_2]').val()); 
    value3 = parseFloat($('input[name=field_3]').val()); 
    value4 = parseFloat($('input[name=field_4]').val()); 
    total  = value1 + value2 + value3 + value4;
    $('input[name=total_field]').val(total);
  });
});

I'm using the jQuery library to keep things simple. The first line checks the page has loaded, the second line will run the code below if any changes are made to field_1 - 4. 

So you'll need to replace field_1, field_2, etc with the names of fields that contains the values that need to be totaled. You can add as many fields as required. 

The final step is to sum up all of the values and add them to the total field. 

Thanks,

Greg

Greg Thomas







PHP Programmer - interactivetools.com

Calculate a sum from a form (update)

By gregThomas - June 24, 2016

Hi Andreas,

I missed the part where they're all list fields. You need to change any instance of input[ to select[, for example:

value3 = parseFloat($('input[name=question_3]').val()); 

would become:

value3 = parseFloat($('select[name=question_3]').val()); 

This includes the values on line 2 that have the change function call.

The only exception might be the final total_question field, as I guess this is probably a text input?

Cheers,

Greg

Greg Thomas







PHP Programmer - interactivetools.com

Calculate a sum from a form (update)

By andreasml - June 25, 2016

Hi Greg

Unfortunately, it does not work. 

I created a new section editor containing only the 5 text fields, named as field_1, field_2, field_3, field_4. I named the fifth text field total_field. Then, i made a new custom.js file exactly as your first script, and i uploaded in the cmsb directory. I expect that in any new record i create, after i complete the field_1 to field_4, the total_field will contain the sum of fields field _1 to field_4. 

Is it correct?

Andreas

Calculate a sum from a form (update 2)

By andreasml - June 26, 2016

Hi Greg

Fortunately, it seems that everything runs well now. Thank you very much for your assistance.

Andreas ML