ResoundingEchoes

The Volume Just Increases

  • Facebook
  • GitHub
  • LinkedIn
  • Twitter
  • YouTube
  • Home
  • Portfolio
  • Blog
  • Interact
    • Say Hello
    • Request A Quote
    • Join Our Newsletter
You are here: Home / Development / Pre-populating Checkboxes in Gravity Forms

Pre-populating Checkboxes in Gravity Forms

March 10, 2017 by Michael Hull 7 Comments

We previously wrote about how you can pre-populate input values in Gravity Forms.  The comments for that post revealed something obvious that we missed – the technique does not work for checkbox fields or any other fields that have pre-defined choices, such as radio buttons and dropdowns.

For this blog post, we are focusing on checkbox fields to keep things simple.  However, based on our testing the same technique will also work for radio buttons and dropdown/select elements.

Hooking Into The Right Place

In our previous post demonstrating how to pre-populate text inputs, we used the gform_field_value_{field_name} hook, in conjunction with the parameter name that we set for the field.

For checkboxes, though, it turns out that we need to use a hook that acts on a higher level and affects the entire form.  The hook we need is gform_pre_render_{id}, where {id} is the form ID being used.  So, if our Form ID were 1, we would use the hook gform_pre_render_1.

Since we’re hooking into the entire form, we have to be really careful and only alter the field(s) and the choice(s) that we want to alter.

Getting The Right Field ID

Each field in Gravity Forms is given its own ID, which will be unique within that specific form.  Take special note of the Field ID for the checkbox field that you want to pre-populate.  In our example shown below, our field is called Favorite Colors and the Field ID is 1.

Illustrating how to find the Field ID within a Gravity Forms field

Setting A Value For Each Choice

Before writing any code, let’s also make sure that we give our choices all a value, instead of relying solely on the field label.

To do this, we simply need to check the “show values” checkbox when editing our form field and then enter a value for each checkbox.

Putting It All Together

Now that we’ve got our checkbox field, we know its Field ID, and we have a value set for each choice, we can add our code which will pre-populate the checkboxes.

In our example, we are pre-populating the “Red” and “Blue” checkboxes and leaving the “Green” checkbox unchecked by default.  Of course, the user can change these as they see fit but this lets us set up the form the way we want initially.

Note that for radio and dropdown fields, only one value is allowed to be selected.  Therefore, in our example the “Blue” choice would be selected but “Red” would not.  This is because “Blue” comes later in the loop and it overrides the fact that “Red” was selected earlier.

Possible Use Cases

You might notice that Gravity Forms allows you to achieve this exact same example without using any code, by checking the checkboxes to the left of each choice within the Admin UI.

However, since our method allows us to insert our own PHP code, we can do things that allow for more dynamic situations such as having certain checkboxes checked by default based on which user is currently logged in.

Filed Under: Development Tagged With: Gravity Forms, PHP

Comments

  1. Johan says

    March 27, 2017 at 9:59 am

    Thank you! Been searching all over the place about how to pre-check checkboxes dynamically! Only other tutorials and snippets I’ve found has been about adding options, nothing about selecting them!

    You saved my day! 🙂

    Reply
    • Michael Hull says

      March 31, 2017 at 10:57 pm

      Hey Johan, I’m very glad this helped you out! Thanks for stopping by and reading.

      Reply
  2. Tiffany Israel says

    May 9, 2017 at 12:15 am

    This code was a total life-saver, thank you! One thing I noticed was that when I add a gravity form shortcode to the confirmation so that the form appears along with a confirmation message, the proper checkboxes aren’t selected. This works for other dynamic population but not for this checkbox solution.

    I can do without it, just curious if you had any insight?

    Reply
  3. Scot MacDonald says

    May 29, 2017 at 6:24 pm

    I have been looking for a solution that will enable me to pass the values of a multi-select field to a text box which could display as a list or a simple comma separated array. Is this something that is possible using the same sort of solution?

    Cheers

    Reply
  4. Wagner says

    June 1, 2017 at 9:57 am

    Hi there!

    I’d like to dynamically check a few checkboxes in my form through $_GET. Is that possible? I mean, our clients fill up a quotation request… this is sent to us by email with a link to our quotation form. This link includes all the information passed to us by the client. I am having no problem to dynamically populate the text fields in the quotation form but the checkboxes are giving me a bit of a headache because we have only one “parameter field” for a whole bunch of boxes.

    Any help would be really appreciated!

    Reply
  5. Josh Holmes says

    June 14, 2017 at 9:34 am

    Hello Michael,

    Thank you for the article. I was just wondering if you have tried this with the addition of conditional logic, where the selected option affects the visibility of other fields.

    I have tried it but the fields which are determined by the selected checkbox do show/hide.

    I look forward to hearing from you.

    Reply
  6. Eric Celeste says

    March 5, 2018 at 5:02 pm

    I’m not sure why the “gform_field_value” hook didn’t do the trick for you. I use this to pre-check checkboxes myself.

    For example, if I have checkboxes with available values of “One”, “Two”, and “Three” I can set the $value in gform_field_value to “One” or to [“One”] or to [“Two”, “One”] or even to “One,Two” and they all seem to check the corresponding boxes.

    Maybe there has been an update to Gravity Forms to make this work? I am using v.2.2.5.

    Reply

Leave a Reply Cancel reply

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

How can we help you?   »

Recent Posts

  • Creating An xy-Plane Component With Vue.js
  • Set Theory And Russell’s Paradox
  • Function And Set Notation
  • What Is A Function
  • My First Peek At Angular 2

Comments From The Blog

  • Eric Celeste on Pre-populating Checkboxes in Gravity FormsI'm not sure why the "gform_field_value" hook didn't do the trick for you. I use this to pre-check checkboxes myself.
  • gpence on Creating A Clock With HTML, CSS, and JavaScriptWhy does your example clock have a zero at the top instead of the roman numeral for 12?
  • Mej Oro on How To Style Draggable Elements To Indicate DraggabilityHi, thank you for having written this article ...but really I totally was not able to make it works: I
  • Jonathan Daggerhart on Function And Set NotationGreat post! This is very enlightening to someone like me who has a self-taught programming background and practically no background

1465 Sand Hill Rd
Suite 1022
Candler, NC 28715

(828) 575-0160 Call us on the phone(828) 575-0160 info@resoundingechoes.net

© 2023 ResoundingEchoes