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 dropdown with custom fields?

  1. I have an enquiry form that appears on every 'product page' which dynamically populates a text box with the product name, so far so good, but I'd like to add a dynamic drop-down to let the user pick a colour from the available colours.

    Colours are stored in an array in a custom field, but I can't figure out how to use this custom field to populate a drop-down. Using the same code for the text field but using an array does not seem to work. Please see my code below and let me know where I am going wrong?

    add_filter("gform_field_value_colour", "populate_list");
    function populate_list($value){
         $choices[] = array(array('text' => 'Colour name', 'value' => 'colour name'));
         $choices[] = array(array('text' => 'Colour name 2', 'value' => 'colour name 2'));
    return $choices;
    }

    Regards
    Shaun

    Posted 12 years ago on Tuesday April 3, 2012 | Permalink
  2. I just started using Gravity Forms and wanted to populate a drop down with a dynamic list of employees who were working at the time the form was submitted. I was running into the same problem as you when I found what needs to be done: you have to pre-populate the form.

    In the following example, replace #FORM_ID# with the form's ID. Also, in the drop down, make sure you set the #CSS_CLASS# to a value in the Advanced > Css Class Name settings and edit the following code, replacing it to match what you put on your Gravity Form settings.

    add_filter('gform_pre_render_#FORM_ID#', 'my_populate_colors');
    function my_populate_colors($form) {
    	foreach($form['fields'] as &$field):
    		if($field['type'] != 'select' || strpos($field['cssClass'], '#CSS_NAME#') === false):
    			continue;
    		else:
    			$choices = array();
    			$choices[] = array('text' => 'Colour name', 'value' => 'colour name');
    			$choices[] = array('text' => 'Colour name 2', 'value' => 'colour name 2');
    			$field['choices'] = $choices;
    		endif;
    	endforeach;
    	return $form;
    }
    Posted 12 years ago on Tuesday April 17, 2012 | Permalink
  3. Thanks for your reply, but this is no good for me.

    As I said above, the form appears on every produce page, but every product has different colours available. If I did your method, it would show options for all the colours on all the products. I need it to dynamically populate the drop-down with only the colours available for the product the person is looking at. These are stored in custom fields.

    Posted 12 years ago on Wednesday April 18, 2012 | Permalink
  4. I was just trying to demonstrate that you need to pre-populate drop downs.

    I'm using this with MySQL calls to populate the drop downs. You could use that or a switch in the 'else' area that builds the list from, say "get_the_ID()".

    Posted 12 years ago on Wednesday April 18, 2012 | Permalink