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 andreasml - June 24, 2016

Hi Greg.

Following your advise i created the following custom.js file, which i have uploaded in the cmsb directory.  In this file, question_1, question_2, question_3... to question_25 are the names of 25 list fields in one certain section editor. Each one of these list fields gets a value between 1 and 7. As it regards the total number i have created a text field named total_question in the same section editor. Should i expect this last text field to automatically gives the sum of fields question_1 to question_25? Unfortunately, this does not happen.

Any suggestions?

Not needing to say how much I appreciate your help.

Andreas ML

$(document).ready(function(){
  $("input[name=question_1], input[name=question_2], input[name=question_3], input[name=question_4], input[name=question_1], input[name=question_2], input[name=question_3], input[name=question_4], input[name=question_5], input[name=question_6], input[name=question_7], input[name=question_8], input[name=question_9], input[name=question_10], input[name=question_11], input[name=question_12], input[name=question_13], input[name=question_14], input[name=question_15], input[name=question_16], input[name=question_17], input[name=question_18], input[name=question_19], input[name=question_20], input[name=question_21], input[name=question_22], input[name=question_23], input[name=question_24], input[name=question_25]").change(function(){
    value1 = parseFloat($('input[name=question_1]').val()); 
    value2 = parseFloat($('input[name=question_2]').val()); 
    value3 = parseFloat($('input[name=question_3]').val()); 
    value4 = parseFloat($('input[name=question_4]').val()); 
    value5 = parseFloat($('input[name=question_5]').val()); 
    value6 = parseFloat($('input[name=question_6]').val()); 
    value7 = parseFloat($('input[name=question_7]').val()); 
    value8 = parseFloat($('input[name=question_8]').val()); 
    value9 = parseFloat($('input[name=question_9]').val()); 
    value10 = parseFloat($('input[name=question_10]').val()); 
    value11 = parseFloat($('input[name=question_11]').val()); 
    value12 = parseFloat($('input[name=question_12]').val()); 
    value13 = parseFloat($('input[name=question_13]').val()); 
    value14 = parseFloat($('input[name=question_14]').val()); 
    value15 = parseFloat($('input[name=question_15]').val()); 
    value16 = parseFloat($('input[name=question_16]').val()); 
    value17 = parseFloat($('input[name=question_17]').val()); 
    value18 = parseFloat($('input[name=question_18]').val()); 
    value19 = parseFloat($('input[name=question_19]').val()); 
    value20 = parseFloat($('input[name=question_20]').val()); 
    value21 = parseFloat($('input[name=question_21]').val()); 
    value22 = parseFloat($('input[name=question_22]').val()); 
    value23 = parseFloat($('input[name=question_23]').val()); 
    value24 = parseFloat($('input[name=question_24]').val()); 
    value25 = parseFloat($('input[name=question_25]').val()); 
    total  = value1 + value2 + value3 + value4 + value5 + value6 + value7 + value8 + value9 + value10 + value11 + value12 + value13 + value14 + value15 + value16 + value17 + value18 + value19 + value20 + value21 + value22 + value23 + value24 + value25;
    $('input[name=total_question]').val(total);
  });
});

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