Skip Navigation

[Resolved] Calculating values from multiple fields of type Checkboxes

This support ticket is created 2 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 3 replies, has 2 voices.

Last updated by Minesh 2 years, 10 months ago.

Assisted by: Minesh.

Author
Posts
#2300849

Hello. I've seen some older tickets about registering a shortcode and performing simple calculations, but I haven't found anything about working with checkboxes (numeric values stored). What I have is:

Checkboxes A
-- Option 1
-- Option 2
-- Option 3

Checkboxes B
-- Option 1
-- Option 2
-- Option 3

I would like to add all the checked values from both fields and get a total. Depending on that total, I would then like to display a new value, i.e. if total is < 50, then display "Not passing score". If >=50, then "Passing score". Ideally I would like the final score to be stored in a separate custom field upon submitting/updating the form. I tried creating an Integer field and see if I can set some conditions, but fields of types Checkboxes don't appear to be available (because they are arrays, I assume).

Thanks!

#2300863

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Can you please share more details that you want to calculate the option values when user submit the form while add/edit post from backend or you are using a frontend post form. If you are using post form to what post type its attached to and please share how you configure the checkboxes at Toolset => Custom fields.

Please share all this required information, once you share that I will review that and then I'll be able to guide you in the right direction.

#2300925
Screenshot 2022-02-23 040710.png

It's a custom post type and the submission is in the back-end. The field format I'm using is in the attached picture. Thanks

#2302197

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

There is no such feature available and it will require custom code.

There are two ways, either you can use Javascript or you can use WordPress hook save_post (please note that save_post hook is not working as expected with Block editor in your case).

You can use the following code and add any plugin that loads the javascript on backend:
=> https://wordpress.org/plugins/custom-css-js/

jQuery(document).ready(function($){
		
	jQuery('input[name^="wpcf[count-options]"]').on("change",function(){
		var sum = 0;
		jQuery('input[name^="wpcf[count-options]"]').each(function(){
			
			 	if(this.checked) {
						sum = sum + parseFloat(this.value);
		  		}
		});
		
		jQuery('input[name^="wpcf[total-field]"]').val(sum); 
		
	});
});

- You can change the custom field slug "count-options" and "total-field".

You can adjust the code for your multiple checkboxes field.