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.

Populate Drop Down dynamically by database Query - Solution.

  1. If you create a simple form with a single dropdown and want to populate it with a database table that contains all 50 states, Copy and paste the code below, replace the Form ID number and tables. It will work. Hope solutions are allowed here.

    add_filter("gform_pre_render_5", populate_dropdown); //5 is the GF Form ID
    add_filter("gform_admin_pre_render_5", populate_dropdown);

    function populate_dropdown($form){
    global $wpdb; //Accessing WP Database (non-WP Table) use code below.
    $results = $wpdb->get_results("SELECT btc_state_short from btc_state_list");

    $choices = array();
    $choices[] = array("text" => "Select a State", "value" => ""); //adding a array option with no value, this will make the user select and option.

    foreach ($results as $result) {
    $choices[] = array("text" => $result->btc_state_short, "value" => $result->btc_state_short);
    }

    foreach($form["fields"] as &$field){
    if($field["id"] == 1){
    $field["choices"] = $choices;
    }
    }

    return $form;
    }

    Posted 11 years ago on Wednesday June 26, 2013 | Permalink
  2. Solutions are def. allowed here, thanks for posting!

    Posted 11 years ago on Friday June 28, 2013 | Permalink

This topic has been resolved and has been closed to new replies.