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.

Pre select Drop Down Option from Dynamically Populated Drop Down Field

  1. theslink2000

    This is kinda a continuation / spin off from a previous thread found here:

    I've got a drop down field that is dynamically populated with a post type using the "populate with post type" option from the advanced menu, but by using this I can no longer preselect an option.

    Specifically I'm trying to populate it with a users existing choice so here's the code:

    //Populate the original medication edit field
    function original_medication_name() {
    	$post_id = $_GET['original_post_id'];  //Pass GET function to pull variable from query string
    	$post_field = "-name";  //Supply the bit of the field name specific to this item
    	$data = populate_edit_field($post_id, $post_field);  //Build full funtion to find the required item of data
    	return $data;
    //Function to build the command to and find the field data
    function populate_edit_field ($post_id, $post_field) {
    	$post_type = get_post_type($post_id);  //Use get_post_type to find the type of post, this is used as the part of the field name that changes for each type, such as "wpcf-surgery-street-address"
    	$database_field = "wpcf-" . $post_type . $post_field;  //Build complete field name
    	$post_data = find_post_data($post_id, $database_field, true);  //Finally find the contents of the field and post it back to the form
    	return $post_data;

    This pulls the existing entry from the database field it's stored in and passes it as the parameter for selection. I know this is treated as the value of the item and the "populate with post type" option sets the post title as both the label and value and the above method works perfectly if the options have been manually added in the editor, they just don't work with the dynamically populated fields.

    I'm guessing this is due to the order in which these hooks are rendered so I'm going to try the pre_render hook and see if it helps. I know it's a bit in depth but it's fully within the scope of Gravity Forms so perhaps this could be added into the documentation, which hasn't been updated for a while as it doesn't even cover the new advanced options for the drop down fields.

    If this is a bug I'd like to report it but I'd like to run it past the forums first.

    Many thanks.

    Posted 10 years ago on Tuesday September 11, 2012 | Permalink