# Prepopulating a select field on a form embedded by function call

**URL:** <https://community.gravityforms.com/t/prepopulating-a-select-field-on-a-form-embedded-by-function-call/1501>\
**Category:** Get Help\
**Tags:** custom, function-call, populate-dropdown\
**Created:** [April 4, 2019, 9:14pm UTC](https://community.gravityforms.com/t/prepopulating-a-select-field-on-a-form-embedded-by-function-call/1501 "2019-04-04T21:14:46Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![user5c06e201cb9060.0](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/user5c06e201cb9060.0/32/611_2.png) [@user5c06e201cb9060.0](https://community.gravityforms.com/u/user5c06e201cb9060.0)\
**Post date:** [April 4, 2019, 9:14pm UTC](https://community.gravityforms.com/t/prepopulating-a-select-field-on-a-form-embedded-by-function-call/1501/1 "2019-04-04T21:14:46Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Mad\_Dog](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/mad_dog/32/159_2.png) [@Mad\_Dog](https://community.gravityforms.com/u/Mad_Dog)\
**Post date:** [April 5, 2019, 4:18pm UTC](https://community.gravityforms.com/t/prepopulating-a-select-field-on-a-form-embedded-by-function-call/1501/2 "2019-04-05T16:18:19Z")

</div>

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

> **[gform\_pre\_render](https://docs.gravityforms.com/gform_pre_render/#1-populate-choices)**
>
> The gform\_pre\_render filter is executed before the form is displayed and can be used to manipulate the Form Object prior to rendering the form.

> **[Dynamically Populating Drop Down, Radio Buttons or Multi-Select Fields](http://docs.gravityforms.com/dynamically-populating-drop-down-or-radio-buttons-fields/)**
>
> The premise for this walkthrough is that we want to populate the options in a Gravity Form select field with all of the posts currently published on the site.

---

<div class="post-metadata">

**Author:** ![user5c06e201cb9060.0](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/user5c06e201cb9060.0/32/611_2.png) [@user5c06e201cb9060.0](https://community.gravityforms.com/u/user5c06e201cb9060.0)\
**Post date:** [April 5, 2019, 4:58pm UTC](https://community.gravityforms.com/t/prepopulating-a-select-field-on-a-form-embedded-by-function-call/1501/3 "2019-04-05T16:58:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Parkbum](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/parkbum/32/810_2.png) [@Parkbum](https://community.gravityforms.com/u/Parkbum)\
**Post date:** [April 5, 2019, 5:16pm UTC](https://community.gravityforms.com/t/prepopulating-a-select-field-on-a-form-embedded-by-function-call/1501/4 "2019-04-05T17:16:13Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![user5c06e201cb9060.0](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/user5c06e201cb9060.0/32/611_2.png) [@user5c06e201cb9060.0](https://community.gravityforms.com/u/user5c06e201cb9060.0)\
**Post date:** [April 5, 2019, 6:43pm UTC](https://community.gravityforms.com/t/prepopulating-a-select-field-on-a-form-embedded-by-function-call/1501/5 "2019-04-05T18:43:08Z")

</div>

On this page: [https://docs.gravityforms.com/embedding-a-form/](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?

---

<div class="post-metadata">

**Author:** ![Parkbum](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/parkbum/32/810_2.png) [@Parkbum](https://community.gravityforms.com/u/Parkbum)\
**Post date:** [April 5, 2019, 9:15pm UTC](https://community.gravityforms.com/t/prepopulating-a-select-field-on-a-form-embedded-by-function-call/1501/6 "2019-04-05T21:15:41Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![user5c06e201cb9060.0](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/user5c06e201cb9060.0/32/611_2.png) [@user5c06e201cb9060.0](https://community.gravityforms.com/u/user5c06e201cb9060.0)\
**Post date:** [April 5, 2019, 10:10pm UTC](https://community.gravityforms.com/t/prepopulating-a-select-field-on-a-form-embedded-by-function-call/1501/7 "2019-04-05T22:10:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Parkbum](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/parkbum/32/810_2.png) [@Parkbum](https://community.gravityforms.com/u/Parkbum)\
**Post date:** [April 8, 2019, 2:52am UTC](https://community.gravityforms.com/t/prepopulating-a-select-field-on-a-form-embedded-by-function-call/1501/8 "2019-04-08T02:52:13Z")

</div>

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/](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!

---

<div class="post-metadata">

**Author:** ![user5c06e201cb9060.0](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/user5c06e201cb9060.0/32/611_2.png) [@user5c06e201cb9060.0](https://community.gravityforms.com/u/user5c06e201cb9060.0)\
**Post date:** [April 8, 2019, 2:59pm UTC](https://community.gravityforms.com/t/prepopulating-a-select-field-on-a-form-embedded-by-function-call/1501/9 "2019-04-08T14:59:23Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Parkbum](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/parkbum/32/810_2.png) [@Parkbum](https://community.gravityforms.com/u/Parkbum)\
**Post date:** [April 8, 2019, 3:26pm UTC](https://community.gravityforms.com/t/prepopulating-a-select-field-on-a-form-embedded-by-function-call/1501/10 "2019-04-08T15:26:31Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![user5c06e201cb9060.0](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/user5c06e201cb9060.0/32/611_2.png) [@user5c06e201cb9060.0](https://community.gravityforms.com/u/user5c06e201cb9060.0)\
**Post date:** [April 8, 2019, 3:54pm UTC](https://community.gravityforms.com/t/prepopulating-a-select-field-on-a-form-embedded-by-function-call/1501/11 "2019-04-08T15:54:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![chrishajer](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/chrishajer/32/88_2.png) [@chrishajer](https://community.gravityforms.com/u/chrishajer)\
**Post date:** [October 23, 2021, 4:20pm UTC](https://community.gravityforms.com/t/prepopulating-a-select-field-on-a-form-embedded-by-function-call/1501/13 "2021-10-23T16:20:22Z")

</div>

Closed on October 23, 2021, 4:20pm UTC


