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.

  1. Overview
  2. Using the Snippet
    1. Prerequisites
    2. Add a Checkbox Field
    3. Add a Number Field
    4. Update Snippet Parameters
    5. That’s It!
  3. Parameters
  4. Taking It Further
    1. Count as Quantities

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.

Comments

  1. Samuel Bassah
    Samuel Bassah Staff April 16, 2024 at 11:15 am

    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,

    Reply
  2. Steve Adams
    Steve Adams April 15, 2024 at 8:54 pm

    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.

    Reply
    1. Samuel Bassah
      Samuel Bassah Staff April 16, 2024 at 6:18 am

      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,

    2. Steve Adams
      Steve Adams April 16, 2024 at 10:34 am

      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.

    1. Samuel Bassah
      Samuel Bassah Staff June 29, 2023 at 9:11 am

      Hi Elias,

      Unfortunately I don’t really understand the question. Can you please provide more information on what you want.

      Best,

  3. Ophélie
    Ophélie November 8, 2022 at 11:22 am

    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

    Reply
    1. Samuel Bassah
      Samuel Bassah Staff November 8, 2022 at 11:28 am

      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,

    2. Samuel Bassah
      Samuel Bassah Staff November 8, 2022 at 12:01 pm

      Hi,

      You’ll have to use a separate Quantity Field and not the quantity input in the Product field.

      Best,

  4. Raven Hobbs
    Raven Hobbs October 3, 2022 at 12:07 pm

    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!

    Reply
    1. Dario Space
      Dario Space October 3, 2022 at 12:27 pm

      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,

  5. Bet Hannon
    Bet Hannon July 19, 2022 at 6:09 pm

    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?

    Reply
    1. Dario Space
      Dario Space July 19, 2022 at 6:13 pm

      Hey Bet,

      We’ve already followed up via email. This will require some digging by our Dev team.

      Best,

  6. Kristy
    Kristy June 8, 2022 at 10:54 pm

    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!

    Reply
  7. Dominic Johnson
    Dominic Johnson May 11, 2022 at 12:43 pm

    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

    Reply
    1. Samuel Bassah
      Samuel Bassah Staff May 11, 2022 at 12:53 pm

      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

  8. Scott Banner
    Scott Banner May 6, 2022 at 2:40 pm

    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?

    Reply
    1. Dario Space
      Dario Space May 6, 2022 at 2:51 pm

      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,

  9. Robert Bollen
    Robert Bollen January 20, 2022 at 2:32 pm

    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?

    Reply
  10. Sandi
    Sandi January 13, 2022 at 2:22 pm

    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.

    Reply
  11. Anne John
    Anne John August 22, 2021 at 10:11 am

    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.

    //////////////////////////////////

    Reply
    1. Samuel Bassah
      Samuel Bassah Staff August 23, 2021 at 6:42 am

      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,

  12. Charlie
    Charlie July 9, 2021 at 1:27 pm

    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.

    Reply
    1. Scott Ryer
      Scott Ryer Staff July 9, 2021 at 2:41 pm

      Hi Charlie,

      Glad to like the snippet and thanks for the feedback. I’m going to pass this along to our dev team.

    1. Samuel Bassah
      Samuel Bassah Staff October 22, 2020 at 10:02 am

      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

    2. Ricardo
      Ricardo February 21, 2021 at 6:59 pm

      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

    3. Ricardo Pint
      Ricardo Pint February 21, 2021 at 7:16 pm

      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.

    4. Samuel Bassah
      Samuel Bassah Staff February 22, 2021 at 11:05 am

      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,

  13. Mauricio Ramirez
    Mauricio Ramirez August 28, 2020 at 10:19 am

    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

    Reply
    1. Ryan Donovan
      Ryan Donovan August 28, 2020 at 10:51 am

      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.

  14. K A
    K A June 24, 2020 at 11:27 am

    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?

    Reply
    1. Ryan Donovan
      Ryan Donovan June 24, 2020 at 12:09 pm

      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.

  15. RG
    RG June 11, 2020 at 12:03 am

    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!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.

Download Snippet

Count the Total Number of Checked Checkboxes with Gravity Forms

This field is for validation purposes and should be left unchanged.