Adam Trickett

Website designer & developer

From one Gravity Form into another: Dynamic drop-down menu

A task that I had to tackle this week was to populate a drop-down list in one Gravity Form with the names of people who filled out another Gravity Form on the same website. Sound like a lot of work? Not really:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
add_filter('gform_pre_render_2', 'rowp_populate_posts');
 
function rowp_populate_posts($form) {
    global $wpdb;
    foreach($form['fields'] as &$field) {
        if($field['type'] != 'select' || strpos($field['cssClass'], 'select-client') === false)
            continue;
        $choices = array(array('text' => 'Select a Client', 'value' => ' '));
        $table = $wpdb->prefix.'rg_lead_detail';
        $clients = $wpdb->get_results("SELECT * FROM $table WHERE (form_id = 1 AND field_number = 9)");
        foreach($clients as $client) {
             $choices[] = array('text' => $client->value, 'value' => $client->value);
        }
        $field['choices'] = $choices;
    }
    return $form;
}
add_filter('gform_pre_render_2', 'rowp_populate_posts');

function rowp_populate_posts($form) {
	global $wpdb;
	foreach($form['fields'] as &$field) {
		if($field['type'] != 'select' || strpos($field['cssClass'], 'select-client') === false)
			continue;
		$choices = array(array('text' => 'Select a Client', 'value' => ' '));
		$table = $wpdb->prefix.'rg_lead_detail';
		$clients = $wpdb->get_results("SELECT * FROM $table WHERE (form_id = 1 AND field_number = 9)");
		foreach($clients as $client) {
			 $choices[] = array('text' => $client->value, 'value' => $client->value);
		}
		$field['choices'] = $choices;
	}
	return $form;
}

I’m a bit strapped for time this week but I’ll try and explain whats going on here.

First you need to two Gravity Forms located somewhere on your site. The form with the drop-down you want to populate needs to have a custom css class on that control(in this example it’s called select-client). The array part is copy-pasta’able so I’ll skip explaining that one for the moment.

The database query is a bit trickier. Fire up PHPMyAdmin (or whatever you use) and look at the table wp_rg_lead_detail. To get the results you need you’ll need to set the query to search by form_id and field_number. Then all you have to do is put the results into an array of arrays and send it back to the filter.

One last note on the add_filter part: the filter itself should actually be gform_pre_render_formID where formID is your form’s ID.

I’ll expand on this post when I get a chance in the next couple of weeks. Until then Merry Xmas.

No Responses

Your email address will not be published.

*

Some basic HTML tags are allowed.