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.

Drop down dynamic population from single line text

  1. Hey,

    I've been struggling with the dynamic population thing for some time. Trying to populate a drop down from a single line text -field.

    What would I need to add to get the dynamic population to work for the 'City' drop down?
    If the City that the form user wants to select is not ready in the drop down list, he would be able to write to the 'City if not in the list' and next user of the form would see that one in the 'City' drop down.
    I tried to follow the guide and read bunch of topics many times, but didn't quite get how I should determine that it takes the text from the 'CIty if not in the list' etc.

    FORM: http://www.vaihtoon.fi/WordPress/share

    Help needed, thanks!

    -Miikka

    Posted 12 years ago on Wednesday December 14, 2011 | Permalink
  2. Anyone?

    I've made couple changes to the names of the fields.. I know this is not too difficult thing but can't get it working!

    What would I need to add to get the dynamic population to work for the 'Kaupunki' drop down?
    If the Kaupunki that the form user wants to select is not ready in the drop down list, he would be able to write to the 'Kaupunki jos ei listalla' and next user of the form would see that one in the 'Kaupunki' drop down.
    I tried to follow the guide and read bunch of topics many times, but didn't quite get how I should determine that it takes the text from the 'Kaupunki jos ei listalla' etc.

    Posted 12 years ago on Sunday December 25, 2011 | Permalink
  3. kyle
    Member

    I was intrigued by this one, so I took a stab at it. Stealing a bit from here and here I came up with the following.

    First.. create your dropdown field. Set the single option text to "Add New City" and the value to '' (blank). Click Advanced, and give it a class of "populate-city".

    Add a second field below that. GIve it a title of New City. Make a note of the field ID. Set the Conditional tag to only show if your dropdown is set to ADD NEW CITY.

    Then, in your functions.php file, add the following:

    <?php
    
    // update the '2' in gform_pre_render_2 to the ID of your form
    add_filter('gform_pre_render_2', 'populate_cities');
    
    function populate_cities($form){
    
        foreach($form['fields'] as &$field){
    
            if($field['type'] != 'select' || strpos($field['cssClass'], 'populate-city') === false)
                continue;
    
            // get previous entries user-added cities
            // update the 4 below to the field ID of your "New City" field
            $cities = get_entry_field_values(4, $form['id']);
    
             // update 'City Dropdown
            $choices = array();
    
            foreach($cities as $city){
                $choices[] = array('text' => $city['value'], 'value' => $city['value']);
            }
            $choices[] = array('text' => 'Add New City', 'value' => '');
            $field['choices'] = $choices;
    
        }
    
        return $form;
    }
    
    function get_entry_field_values($field_id, $form_id) {
        global $wpdb;
    
        $tablename = $wpdb->prefix . 'rg_lead_detail';
    
        return $wpdb->get_results($wpdb->prepare("SELECT value FROM $tablename WHERE form_id = %d AND field_number = %d", $form_id, $field_id), ARRAY_A);         
    
    }
    ?>

    You may want to sort the choices array, and check for duplicates, not to mention make sure the formatting [capitalization, etc.] is correct, but I'll leave that up to you. When someone goes to fill out the form, they'll see a list of previously added cities. They can choose that or choose "ADD NEW CITY" and use the input field that appears to enter their city name.

    Hope it helps.

    Posted 12 years ago on Sunday December 25, 2011 | Permalink