PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

Dynamically populate drop down with user info

  1. All of the tutorials I am seeing for dynamically populating a drop down aim to replace the drop down options with a set of data (like adding certain posts to the drop down). What I am trying to do is dynamically choose an option from the drop down based on previously entered user information.

    For instance, I have set up a custom user field for country. When the user registers, they choose the country they are in from a drop down. I am trying to have my gravity forms automatically choose the country that they selected upon registration, but leave the defined drop down options available. I have been able to dynamically populate text fields with custom user information with a function like this:

    // populate the field with "user_title" as the population parameter with the "user_title" of the current user
    function env_populate_title() {
    	$s2title = get_user_field ("title");
    	if ($s2title == '') { $value = populate_usermeta('user_title'); }
    	else { $value = $s2title; }
    	return $value;
    }
    add_filter('gform_field_value_user_title', 'env_populate_title' );

    I know I need to pre-render the form to apply the drop down value but I'm not sure how to just select the value and not replace the drop down choices. I know that this is not there yet but it should be something similar to this:

    function env_populate_country($form) {
    	foreach($form['fields'] as &$field):
    		if($field['type'] != 'select' || strpos($field['cssClass'], 'country') === false):
    			continue;
    		else:
    			$choices = get_user_field ("country");
    			$field['choices'] = $choices;
    		endif;
    	endforeach;
    	return $form;
    }
    add_filter('gform_pre_render', 'env_populate_country');

    Please help...

    Posted 12 years ago on Friday July 27, 2012 | Permalink
  2. David Peralty

    You need an action to happen that Gravity Forms can hook into. There are two options: multi-page form or JQuery. If you use the multi-page form, you can hook into the page change action to pre-populate a dropdown based on previous user entered data. If you chose JQuery, you can hook into the on change action and have the data in the dropdown change. The JQuery part is way outside my expertise currently.

    Here is the hook for post paging:
    http://www.gravityhelp.com/documentation/page/Gform_post_paging

    Posted 12 years ago on Friday July 27, 2012 | Permalink
  3. This needs to apply to all forms, none of which are multiple page forms.
    Any chance there is someone that can help me with the jquery option? Without knowing more, I don't really know what you mean by hooking to the on change action. Exactly what is changing within the jquery realm that would open the possibility to hook to it? Where/when is the on change action happening? I can't even begin to guess at how that would work.

    Posted 12 years ago on Friday July 27, 2012 | Permalink
  4. David Peralty

    I just saw your Priority support request and realized you aren't trying to do what I thought. I will discuss this with you there.

    Posted 12 years ago on Friday July 27, 2012 | Permalink
  5. For the benefit of other user who might be trying to accomplish the same thing, this function does work to pre-select the country dropdown.

    /* populate the field with "user_country" as the population parameter with the "user_country" of the current user*/
    function env_populate_country() {
    	$s2country = get_user_field ("country");
    	if ($s2country == '') { $value = populate_usermeta('user_country'); }
    	else { $value = $s2country; }
    	return $value;
    }
    add_filter('gform_field_value_user_country', 'env_populate_country' );

    I wasn't seeing it at first, but I logged out of my user and logged back in and it kicked in. Also, just some advice separate from this problem, make sure that the values and labels match between the user registration drop down (or where ever you collect the data) and the drop down that you are trying to populate. For countries especially, I've found that those values can vary.

    Posted 12 years ago on Tuesday July 31, 2012 | Permalink