So recently someone asked me, "Can Gravity Forms pre-populate a dropdown [radio or select] with information based on a previous input in a multipage form?" While the options are plenty to make this happen with the very many Gravity Form hooks. Some include the following (but not limited to):
- Set a cookie & retrieve the cookie for use later using setcookie() & $_COOKIE.
- Use jQuery (and/or AJAX) to populate fields
While both of these are extremely viable options, what this person wanted was to search a custom post type for someone's name. So on page 1 of the form was an input for the person's name.
While logically, one would think that the gform_field_value_$parameter_name filter would work, I couldn't for the life of me determine how to do this for select and/or radio fields. So, I switched back to gform_pre_render hook. As you may or may not know gform_pre_render hook gives you the $form object to manipulate prior to rendering the form and executed before the form is displayed, which is what I needed.
Setup
In order to set this up, you will need two things:
- A custom post type called 'Persons' (wps_person), which served as the body of data that would pre-populate the radios.
- A simple Multipaged form
Gravity Forms Multi-paged Form
So create a new gravity form. Add a name (Field ID:1), then add a radio field. In the name field select either Normal (this is what the example uses) or Simple (example has a commented out replacement for this). Have the name on page 1 and the radio options on page 2. In the radio field, do the following under the Advanced Tab:
- Add a CSS class name 'person'
- Check "Allow field to be populated dynamically" but no need to choose a parameter name
Now you are ready...
Gravity Forms Customization
First, we have to cycle through the form fields. Gravity Forms does not prepped the Form Object (here $form) where the key is the Field ID (though that would be nice).
Next, in this snippet, you will notice a check at the beginning to see if we are on the correct field. You may think I go overboard, but I don't want to edit the wrong field. So I am checking for the following:
- Field Type
- CSS class
- Allows pre-population
[php]
if ( 'radio' != $field['type'] || false === strpos( $field['cssClass'], 'candle-person' ) || ! rgar( $field, 'allowsPrepopulate' ) )
[/php]
While you don't necessarily need all three per se, I find that I don't run into errors when I do all three.
Third, we do a new WP_Query() (see codex for more information & see Mark Luetke's (Github, Site) WP_Query Args Gist) searching for those items that match the name. Here I access the $_POST var with the appropriate input_*_* name. I then also tell it to query my post type ('wps_person'). For future possibilities, I add another query var 'gform_search', just in case I want to do something with pre_get_posts later, but that's for more advanced usage.
Then, I just cycle through the available posts to create my choices, and return the form.
Here's an example:
Pippin says
That’s awesome.
rob says
Thanks for this! Can I just tell gravity forms which dropdown item to take when I implement it via shortcode? For example I want to have 1 contact form with a dropdown field offering 20 subjects. When the User is on the page “shopping support”, the dropdown field should take “shopping”. When he is on the page “consulting”, the same dropdown field should be the “consulting” item. Is that possible? I just tried to to it like that:
[gravityform id="3" name="Contact" title="false" description="false" ajax="true" question="support"]
question is the parameter name from the gravity forms backend.
Travis Smith says
Hello Rob,
If you set the dynamic parameter in Gravity Forms as question, and then refer people to the page with a query arg (http://domain.com/contact/?question=support) it will auto select that field for you.
To pass it via shortcode, simply do something like this: [ gravityform id=”3″ name=”Contact” title=”false” description=”false” ajax=”true” field_values=”parameter_name=question&val=support”]
If your form exists in the sidebar or whatever, then you may need to have a function. Gravity Forms has some great documentation now, so check out the following: Dynamically Populating Drop Down Fields, Allow field to be populated dynamically, and Using Dynamic Population
Patrick says
Is there anyway to dynamically populate a drop down box, check boxes, or radio buttons with just shortcode?
Travis Smith says
No, it’s a bit more complicated than that.
Red says
Hi,
Is it possible to populate a set of radio buttons based on another set of radio buttons? The radio buttons are in the same form.
So let’s say I have 1 set which is
Radio Buttons 1
* Red
* Green
* Blue
Radio Buttons 2
* 1
* 2
* 3
So I would like to do a direct mapping. If a user chooses “red” then “1” is dynamically chosen in the radio button 2 set. The Radio button 2 set is hidden as an admin field.
I am sure this is possible but not sure how to do it
ANy help?
Cheers Red
Travis Smith says
Hello Red,
Yes, you would need to bind some JS to make this happen. Otherwise you are stuck with validating and sanitizing in PHP after or upon submission prior to the form hitting the database.
Thanks,
Travis
Ty says
I’m looking for something similar. Except in my version, selecting “red” in area 1 would automatically select “1” in area 2, but still give you the option to select others in area 2. You mentioned it would need some “JS” but can you point to or describe exactly how this could be done. Explaining it “for dummies” would be most helpful. Thanks!
Chuck Ashton says
Hi Travis,
I’m using a gravity form on our site and I can’t figure out how to dynamically populate the date fields. I have two date fields in the form we’re using for reservations. One is the arrival date which I would like to default to the current date. The Departure date field I would like to default to the date input into the arrival date field plus 7 days.
Is this sort of thing possible?
Thanks,
Chuck
Travis Smith says
Hello Chuck,
Yes, read here: http://www.gravityhelp.com/documentation/page/Gform_field_value_$parameter_name
Thanks,
Travis
Chuck Ashton says
Thanks Travis,
I was aware of that help document, it explains how to set the date field to a pre-determined date. My question was more about dynamically populating the date field to the current date, ie – the date of which the visitor is on the site.
Is this much more complicated or is it simply changing the returned value somehow?
Again, thanks for your help thus far.
Chuck
Travis Smith says
Yes sir, just change the return value. Use
date()
for the current date and see the formats here. You can also use mktime() or strtotime() for the 1 week later date. Something like this$date = strtotime(date("Y-m-d", strtotime($date)) . " +1 week");
or$now = new DateTime(); echo $now->add(new DateInterval('P1W'))->format('Y-m-d');
for PHP 5.3.x.brian says
Any ideas how to dynamically populate ‘End Time & Date’ based on the ‘Start Date & Time’ values? For example, “End Date” should be the same date as the “Start Date” and “End Time” should be “Start Time +1 hour”.
Jonathon says
Travis,
Great info, thanks for posting.
Would I be correct to assume that this could be revised to auto populate the radio button labels with featured images from the custom post type? If so, any tips?
Looking for a way to keep track of graphics that would each have descriptions and additional data (hence the custom post type) that the user would select from – the graphics will change out regularly, so dynamically populating the radio button selections with visual choices would be perfect if possible.
Thanks,
Jonathon
Jonathon Harris says
Hi Travis,
Any way this could be adapted to dynamically pull in featured images from a custom post type -> as image selections inside of a radio button field? Any advice appreciated – thank you for the post, GF seems to have a lot of control “under the hood”.
Jonathon
David says
I am trying to populate default selections in accessory drop downs when a user selects a specific length.
Drop Down A: Length In Feet/Inches of Covers
Drop Down B: Length Of Internal Element
Drop Down C: Number Of Support Brackets
Both B and C are based on what they chose in A. I don’t want to leave it to the user to have to select all drop downs and do the math themselves. I want them to select a length in A and the rest is done for them.
I thought about doing this with calculations but when the order goes to the factory it does not properly show the specifications of the order.
Any help would be great. I can pay for a solution also. 🙂
Karan says
Awesome work! Help me a lot. Thanks
Paul Christian says
Nice article, great tips, but I have just one question. Can I ‘pre select’ a radio button without that ‘pre selection’ coming from a previous entry? Just like this:
Person sees contact form > Contact via email is pre selected, but under it is an empty radio button in which they can select ‘I prefer contact via phone’…. When clicked, the ‘contact via mail’ is empty, and the ‘via phone’ is ‘fiilled in’…. is that possibe? Like a default setting sort of thing?
Hope you can help me out, I couldn’t find it in the GF settings…
Paul says
Hi, thanks for this post, really helpful as I’m also trying to pre-populate a user defined price from a previous dropdown field and using the shortcode above I think I can see how to do it now!
Norcross says
the normal setup the field_value_$paramater works assuming you use the values / labels setup for the form field