Prepopulating a select field on a form embedded by function call

I have a form that is embedded with a function call and I’m pre-populating the form fields. I can’t figure out how to pre-populate a select field (drop-down box) on the form.

Is it even possible?

See if these help to get you on the right track:

I read those pages but I don’t think they’re quite what I’m trying to do. I’m trying to use a function call in a template like this:

gravity_form($id_or_title, $display_title= true, $display_description= true, $display_inactive= false, $field_values= null, $ajax= false, $tabindex, $echo= true );

I’m trying to populate the $field_values variable correctly. I can’t find any documentation for how to pass prepopulated select box values in the $field_values variable.

What are you trying to populate it with? The Render Examples are of Wordpress posts. If you are trying to populate it with entry data from another form, it should look more like this:

add_filter(“gform_pre_render”, “populate_zone”);
add_filter(“gform_admin_pre_render”, “populate_zone”);
function populate_zone($form){
//CHANGE TO THE TARGET FORM ID
if( $form[“id”] != 14 )

     return $form;

$items = array();

// CHANGE TO THE FORM ID WHERE THE ENTRIES ARE LOCATED
$form_id = ‘7’;
$entries = GFAPI::get_entries( $form_id );

if (is_array($entries))
{
// CHANGE ‘10’ TO THE FIELD ID THAT HAS THE ENTRIES YOU WANT
foreach($entries as $entry) $items[] = array( ‘value’ => rgar( $entry, ‘10’ ), ‘text’ => rgar( $entry, ‘10’ ) );
}
foreach($form[“fields”] as &$field)
// CHANGE TO THE TARGET FIELD ID
if($field[“id”] == 2){
$field[“choices”] = $items;
}
return $form;
}

Don’t forget to make the function name ( populate_zone ) unique for each time to recreate this. Then place the function name into the Dynamic Population part of the Field Advanced Settings.

On this page: https://docs.gravityforms.com/embedding-a-form/

It talks about how you can embed a form with a function call:
gravity_form($id_or_title,$display_title= true,$display_description= true,$display_inactive= false,$field_values= null,$ajax= false,$tabindex,$echo= true );

An example is this:
gravity_form( 1, false, false, false, ‘’, false );

If you say …
$prepop_field_values = array(‘email’=>‘me@example.com’, ‘name’=>‘Tom Jones’);
gravity_form( 1, false, false, false, $prepop_field_values, false );

… then form ID 1 will have the email and name fields prepopulated.

So my questions are:

  1. Can you prepopulate a select box in this way?
  2. If so, how do you format the select box options to pass it to the function call?

I seeeeeee… I’d have to play around with it to see if that is possible, but why do that when there are more obvious/easier/possible ways?

In any case, you may already know this, but just in case… gravity_form( 1, false, false, false, ‘’, false ); is meant to be interested directly into a Template Part. So you would need to go to your current Theme, find the file with the Templates, maybe make a custom Page/Post Template, and then insert the gravity_form code into the place you want it. After that, it would be a matter of playing around with it to get it to populate the way you want it to populate… but again, using Pre_Render will probably be easier.

If you only want it to populate a certain way with certain roles, or certain pages, then its easy enough to just add another Conditional Statement to your Pre_Render.

Hope these thoughts help you get you where you are going!

Thanks for the input.

I’m adding the function call in a custom template that is used by more than one page. Based on the ID of the page, I run database queries against a CRM that is separate from WordPress. The results of the database query prepopulate some of the form fields.

In particular, I’m trying to populate a select field with the names of around 10 officers of the association I work for that are constantly changing and would be a burden to manually update in the form builder admin page.

So are you writing the API for the CRM or is that a separate plugin?

If you were going to us Pre-Render, I think you would use $id = get_the_ID();

https://developer.wordpress.org/reference/functions/get_the_id/

And then a new condition before everything: if( $id != 1 ){ return $form }

Then the tough part. If your CRM is outside your database, you would then need to use the CRM defined API to get the data you want. If it is within your Database, then its easy peezy.

Best of luck!

Everything is figured out about this project except how to populate a select field. I’ve got the data that I want to put in a select field in the template already.

What I need to know is:

  1. Is it possible to prepopulate a select field using the function call?
  2. If so, how do I save the select field info in the $field_values variable? List, array, etc? What does that look like?

How is the data from your CRM retrieved? If it is before the form is created, then you can use a function (either pre_render or gform_field_value). If it is while the user is filling out the form, then you need to use AJAX to call the function you want.

Is your drop down in a List Field? That is handled VERY different than a normal drop down.

Thanks for the help.

I run an SQL query with a web service provided by the CRM to retrieve a list of about 6 first and last names along with the name of a state (eg Ohio, Vermont) and a dollar amount (eg 1234.56, 918.43). I pass these values to the $field_values argument in the gravity_form() function.

This is a basic select field, not a list.

I successfully prepopulate the state and dollar amount without any pre-rendering or filtering. The gravity_form() function expects strings for these fields.

I can’t figure out how to format the select field contents. Say I have 3 names: Nancy Smith, Jane Austin, and Bob Clark. How do I format these names in the $field_values variable? I can save these as any type of PHP variable, I just need to know what gravity_form() is expecting.