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
- Taking It Further
Using 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.
Update Snippet Parameters
choice_field_id in the snippet to match your Form ID, the Number field you’re populating with the count, and any Checkbox fields you want to count.
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. ) );
Your form will now automatically count the number of checked boxes and populate it into the Number field.
- 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.
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.