Sorry to say, but it didn't work :(
I have this to populate the drop down (this works)
add_filter( 'gform_pre_render_1', 'populate_dropdown' );
function populate_dropdown($form){
$items = array();
$items[] = array("text" => "Canada", "value" => "CA");
$items[] = array("text" => "France", "value" => "FR");
$items[] = array("text" => "Germany", "value" => "DE");
$items[] = array("text" => "Netherlands", "value" => "NL");
$items[] = array("text" => "United Kingdom", "value" => "GB");
$items[] = array("text" => "United States", "value" => "US");
foreach($form["fields"] as &$field)
if($field["id"] == 6){
$field["type"] = "select";
$field["choices"] = $items;
}
return $form;
}
Now, to pre-select the drop down the following code SHOULD work, but it doesn't:
add_filter("gform_field_value_country", "populate_country");
function populate_country(){
$country_value = 'US'; // for testing purposes I hard-coded setting the value
return $country_value;
}
But what SHOULD NOT work, but did work is the following:
add_filter("gform_field_value_country", "populate_country");
function populate_country(){
$country_value = 'United States'; // for testing purposes I hard-coded setting the value
return $country_value;
}
Unfortunately I only have the real value (US, UK, NL, etc.) to select the correct one.
So here's what did work: I added the following after
$items[] = array("text" => "United States", "value" => "US");
foreach($items as &$country )
if ($country["value"] == 'US' {
$country = array("text" => $country["text"], "value" => $country["value"], "isSelected"=> true);
}
Note: I'm not a PHP coder, I'm a code thief ;) so some copy-pasting and adding some common sense/logic takes me a long way. I did get the result I wanted BUT what I don't know is if the code will bring a heavy strain on the server or wreck my Gravity form in any way.
So can you please check if my code gets your OK?
Thanks,
Erwin
Posted 14 years ago on Thursday February 11, 2010 |
Permalink