Count the Total Number of Checked Checkboxes with Gravity Forms
April 23, 2021: Fixed a compatibility issue with GP Populate Anything.
Overview
There’s a classic Gravity Forms tongue twister that I love:
How many checks could a Checkbox count if a Checkbox could count checks?
Up until today, the answer to that question was 0 because it was entirely rhetorical. After today, that is no longer the case. The answer is now “all of them”.
This snippet gets the total number of checked Checkboxes and dynamically populates that total into a Number field on the form. You can use this to apply Conditional Logic based on the number of checkboxes checked, populate the number into a Quantity field to calculate Product purchases, and more! Let’s get started.
Using the Snippet
Prerequisites
Confirm that you have Gravity Forms installed and activated and that you’ve installed the snippet.
Add a Checkbox Field
As the name of the snippet implies, we need a Checkbox field to count. Add one to your form.
Add a Number Field
Add a Number Field to the form.
The snippet accepts any field that supports numbers, such as Quantity fields. More on using the snippet to calculate Product Quantities in the Count as Quantities section below.
Update Snippet Parameters
Update the form_id
, count_field_id
, choice_field_id
, and values
 in the snippet to match your Form ID, the Number field you’re populating with the count, the Checkbox field(s) you want to count, and any specific values to which the count should be restricted.
# Configuration
new GW_Choice_Count( array(
'form_id' => 123, // The ID of your form.
'count_field_id' => 4, // Any Number field on your form in which the number of checked checkboxes should be dynamically populated; you can configure conditional logic based on the value of this field.
That’s It!
Your form will now automatically count the number of checked boxes and populate it into the Number field.
Parameters
- form_id (integer) (required) This is the Form ID that contains your Checkbox field. There is no default value.
- count_field_id (integer) (required) This is the Field ID for the Number field in which the number of checked boxes should be dynamically populated. There is no default value.
- choice_field_ids (array) (required) This is an array of Field IDs which should be counted. There is no default value.
- values (array) (optional) This is an array of checkbox values that will be counted when checked. Default value is `false` which will count all values.
Taking It Further
Count as Quantities
The snippet supports populating the Checkbox count into any field that supports Numbers, which includes Quantity fields. Paired with a Product field, the Checkbox count can be used for simple pricing calculations.
To do this, first add a separate Quantity field to your form and map it to your Product field.
With that place, configure the snippet to use the Quantity field for the count_field_id
parameter. Once in place, any checked box automatically adds to the Quantity in the form.
Use GF Read Only to prevent users from overriding the populated Quantity.
Hi Steve,
The number field should be dynamically populated with the count, as the checkbox choices are being selected. We’ll need to take a look at your setup to know exactly why it isn’t working for you, which is why I requested you submit a ticket. It could be something minor you’re missing, but until we take a look at the setup, we won’t be able to know why it isn’t working on your website.
Best,
I added the script to Code Snippet, but it doesn’t appear to be running. The number field isn’t being updated with the count. When I query the countfieldid in the inspect panel it says its not defined. I am wondering if I am not running the script in the right place.
Hi Steve,
I just tested snippet and can confirm that it still works. This is a PHP snippet so you’ll need to either add it to your function.php file or use one of the code snippet plugins to add it to your website, and update the configuration at the bottom of the snippet to the respective IDs. However, if it’s still not working for you, you’ll need to submit a ticket via our support form so we look into this further on your website.
Best,
Thanks Samuel. I am doing something wrong. I made a test form. One checkbox and one number field. Does the number field need to be dynamically populated? Is there a parameter? The script is loaded in my code snippets and all of the fields are correctly set (only 2). I am missing something.
How can I set whether a question has single selection or multiple selection?
Hi Elias,
Unfortunately I don’t really understand the question. Can you please provide more information on what you want.
Best,
Hello,
I can’t integrate the code: https://github.com/gravitywiz/snippet-library/blob/master/gravity-forms/gw-choice-counter.php
Do I only have to configure this part with the IDs of my fields:
new GW_Choice_Count( array( ‘form_id’ => 123, // The ID of your form. ‘count_field_id’ => 4, // Any Number field on your form in which the number of checked checkboxes should be dynamically populated; you can configure conditional logic based on the value of this field. ‘choice_field_ids’ => array( 5, 6 ), // Any array of Checkbox or Multi-select field IDs which should be counted. ) );
Or should I customize the whole code
Thank you
Hi Ophélie,
You only have to change the IDs in the configuration and that should work. I just tested the snippet and it works as expected. If it still not working for you, then we’ll have to take a look at your setup. If you have an active Gravity Perks license, you can get in touch with us via our support form so we can assist you further.
Best,
Thank you! It works :)
I can’t set up my quantity field. How can I activate product field mapping?
Hi,
You’ll have to use a separate Quantity Field and not the quantity input in the Product field.
Best,
Yes I did, but it doesn’t work. The quantity field remains empty even though I check the boxes..
Oh, it works now!
Thanks again :)
You’re welcome.
Best,
Hi, how can i use this snippet if I have multiple checkbox fields on a form and I need to add each field’s counts?
I would be happy to engage in paying y’all for help or code.
Thanks!
Hi Raven,
You can find an Instruction Video about setting up the snippet here. You can probably add a Number field set to Calculation to sum all the counts in your form.
If you have an active Gravity Perks license, can you get in touch with us via our support form.
Best,
I’m working with a client on a survey. Is it possible to adapt this snippet so that I don’t get a count of the number of checks and INDIVIDUAL makes, but rather (using your example in the post where Canoeing and Archery were checkboxes) – I get a total count overall entries that tells me how many times Canoeing and how many times Archery were checked?
Or what would be the easiest way to get that information?
Sorry for the typos — was dictating.😬
Hey Bet,
We’ve already followed up via email. This will require some digging by our Dev team.
Best,
I had to get on here and say THANK YOU, THANK YOU, THANK YOU! I have needed this snippet for YEARS, and I just plugged it into the functions.php file and, voila, my dreams became reality!
Hi Kristy,
You’re welcome. Glad the snippet worked for you.
Cheers,
Hello. Sorry if this is a daft question, but how do I get the field ids for a checkbox. I have a checkbox ID = 46 with 3 fields with values 1,2,3.
‘choice_field_ids’ => array( 5, 6 ), // Any array of Checkbox or Multi-select field IDs which should be counted.
I want to count the number of these checkboxes selected.
When I set this box to 46 and add a select all box – I get the correct count (3) only when I select SELECT ALL.
https://www.oilandgasautomationandtechnology.com/reg-dj/
So what are the field ids of the 3 checkboxes pls?
Thanks
Hi Dominic,
The Checkbox field ID is 46 and should work. We’ll have to dig into this further. I’ve sent you an email to request more information. Please check and reply.
Best
This is the perfect solution for me but when I paste the snippet into my functions.php file and reload, I get a critical error. I even pasted it on without changing anything and still got an error. I’m using the Divi theme. I also tried to use a code injector plugin and updated the ID’s – no error but no results either. What am I doing wrong?
Hi Scott,
Please check that you have added the code Snippet and after that the configuration. If you still have issues and if you have a Gravity Perks license, can you get in touch with us via our support form?
Cheers,
For a charity event I’m trying to make a donation form as follows.
People can select multiple numbers in a ‘multi select’ field. If a person selects 5 numbers, they have to pay 5 x a fixed price per number.
Almost similar but with a multi select field. Is there a way to make the snippet work for my case?
This is a great snippet. I need to do the same thing for a series of radio buttons though. Could I simply swap out references to checkboxes with radios in the snippet to get this to work?
The form needs to limit users to 1 choice per question so checkboxes won’t work for us.
Hi Sandi,
We’ve already followed up via email.
You could use Gravity Forms Limit Checkboxes perk to set one choice per field and use the snippet to count how many checkboxes are checked.
I hope this helps!
Best,
Hey, I was trying this snippet and I pasted this snippet in the funtions.php file at the bottom but when I click ‘update’ The file doesn’t updates. So, then I installed ‘Code snippets’ plugin and created a new snippet and pasted the snippet and when I clicked ‘save change and activate’ it is showing me an error. I’m not a programmer so please help me. The error is mentioned below. Thanks in advance.
ERROR MSG /////////////////////////////////
Don’t Panic
The code snippet you are trying to save produced a fatal error on line 1: Uncaught Error: Class ‘GW_Choice_Count’ not found in /home/andalelatinogril/public_html/wp-content/plugins/code-snippets/php/admin-menus/class-edit-menu.php(221) : eval()’d code:1 Stack trace: #0 /home/andalelatinogril/public_html/wp-content/plugins/code-snippets/php/admin-menus/class-edit-menu.php(221): eval() #1 /home/andalelatinogril/public_html/wp-content/plugins/code-snippets/php/admin-menus/class-edit-menu.php(271): Code_Snippets_Edit_Menu->test_code(Object(Code_Snippet)) #2 /home/andalelatinogril/public_html/wp-content/plugins/code-snippets/php/admin-menus/class-edit-menu.php(130): Code_Snippets_Edit_Menu->save_posted_snippet() #3 /home/andalelatinogril/public_html/wp-content/plugins/code-snippets/php/admin-menus/class-edit-menu.php(107): Code_Snippets_Edit_Menu->process_actions() #4 /home/andalelatinogril/public_html/wp-includes/class-wp-hook.php(303): Code_Snippets_Edit_Menu->load(”) #5 /home/andalelatinogril/public_html/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters(”, Array) #6 /home/
The previous version of the snippet is unchanged, and the rest of this site should be functioning normally as before.
Please use the back button in your browser to return to the previous page and try to fix the code error. If you prefer, you can close this page and discard the changes you just made. No changes will be made to this site.
//////////////////////////////////
Hi Anne,
Looking at the error message, it appears you’ve only added the snippet configuration to your website. You’ve left out the actual snippet which you’ll find here
Could you check on that and see if you’re able to get it working. Here is also documentation on how to install a snippet.
Best,
This is a very useful snippet. It would be great to see it as a Perk that could be edited via the admin without having to edit the snippet each time you need it. If you could add the field choice ids to the count field directly less technical people who don’t want to touch code could use it.
Hi Charlie,
Glad to like the snippet and thanks for the feedback. I’m going to pass this along to our dev team.
Is it possible to only count a specific checkbox item, instead of all those on a list?
Hi Sven,
To count the number of checked checkboxes for just one field, enter only the ID of that Checkbox field into the array for the parameter. So you will have something like this as the parameter:
'choice_field_ids' => array( 388)
Best
I have the same question and didnt understand this answer. Doesnt solution still count all the checkboxes inside the checkbox field with ID 388? Thank you
Update: Slightly less elegant but i managed to make it work by making a “checkbox list” with only one “checkmark item” each. Then, id remove the title of every checkbox list and hide field legend visibility.
Hi Sven,
You should be able to use only one Checkbox field with all the options and pass the ID of that checkbox as the only parameter in the array. So it’s a bit strange, that didn’t work for you. If you have a Gravity Perks License, you can send us a message via the support form and we can take a closer look at your form set up and assist you to get it working.
Best,
Hello,
So to make sure I get this, If I have two fields within the same form where I want to count the number of checkbox.
I add this at the end of my function.php file ? <?php
new GW_Sample_Class( array( ‘form_id’ => 1, ‘target_field_id’ => 7, ”choice_field_ids” => ’10’ ) );
new GW_Sample_Class( array( ‘form_id’ => 2, ‘target_field_id’ => 3, ”choice_field_ids” => ‘8’ ) );
Or do I replace this existing code with this one on top? new GW_Choice_Count( array( ‘form_id’ => 295, ‘count_field_id’ => 9, ‘choice_field_ids’ => array( 6,) // ) );
Thanks
Hello Mauricio, you can actually add multiple variations of the same snippet. Check out our documentation here on how to add a class-based snippet to multiple forms or the same form. That way you can create two instances on the same form and have two count fields much like the first example you displayed. Another alternative would be to have the count shown on one field and then use Gravity Forms Copy Cat to copy the total into another field.
I have a multi step form. I want only the checked employ names in step 2 (others should be hidden), is it possible with this snippet?
Hello, Yes you can. Just make sure to modify the ‘checkbox_field_ids’ to match the checked employee names field and you should be good to go.
Thanks so much for this snippet. Just what I needed. Only question is, how can I make this work sitewide for the same field ids but different form ids?
Use Case: I have a site with many pages, each for a different cause, and they each have a unique but very similar form that has many of the same fields. Visitors choose the cause/page they want and check off the options they’d like for that cause. I want to count the checked boxed for each form, not just one.
Thanks!
Hello, you can actually add multiple variations of the same snippet. Check out our documentation here on how to add a class based snippet to multiple forms or the same form. 😃