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.

Gform_pre_render help

  1. Simply trying to populate a drop down from a custom table.

    Table Name: abc-state-list
    Column Name: state_short
    Form 1
    Field ID 1

    Can anyone help with the code to make this happen in functions.php? The complete code?

    Ive read so many submissions and on the web, all reference the help docs but they don't include any help on a sql queries as well.

    Thank you so much for helping out. It shouldn't be difficult, new to wordpress not to the web.

    Posted 11 years ago on Wednesday June 26, 2013 | Permalink
  2. David Peralty

    I don't think anyone is going to give you the database query. But you could use http://www.gravityhelp.com/documentation/page/Gform_column_input to load the values into a drop down field. The example code should get you very close to what you need.

    Posted 11 years ago on Wednesday June 26, 2013 | Permalink
  3. Hi David,

    Thank you for the response. I am not trying to change the input type just populate the current type (dropdown) with data in a specific table column.

    Posted 11 years ago on Wednesday June 26, 2013 | Permalink
  4. David Peralty

    Sorry, wrong documentation page. http://www.gravityhelp.com/documentation/page/Gform_field_choices Is what you require.

    Posted 11 years ago on Wednesday June 26, 2013 | Permalink
  5. Hello,

    This documentation is for checkbox selections. Below is my code, it will not return the States from the table. Help would be greatly appreciated?

    add_filter("gform_pre_render_5", populate_dropdown);
    add_filter("gform_admin_pre_render", populate_dropdown);

    function populate_dropdown($form){
    global $wpdb;

    if($form["id"] != 5)
    return $form;

    $mytable = $wpdb->btc_state_list;
    $q = "SELECT btc_state_short from $mytable";
    $results = $wpdb->get_results($q);

    $items = array();
    $items[] = array("text" => "", "value" => "");

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

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

    return $form;
    }

    Posted 11 years ago on Wednesday June 26, 2013 | Permalink
  6. David Peralty

    Can you isolate the issue by filling the items array manually with some values to test to see if that part of your code is working as expected?

    Posted 11 years ago on Wednesday June 26, 2013 | Permalink
  7. I cannot narrow this one down and am looking for your support.

    Posted 11 years ago on Wednesday June 26, 2013 | Permalink
  8. OK. I have it narrowed down, I can manuall add choices but cannot retreive table data. Here is where I am at.

    add_filter("gform_pre_render_5", populate_dropdown);
    add_filter("gform_admin_pre_render_5", populate_dropdown);

    function populate_dropdown($form){
    global $wpdb;

    $results = $wpdb->get_row("SELECT btc_state_short from btc_state_list");
    $choices = array();
    $choices[] = array("text" => "2323", "value" => "2323");

    // This is where I am stuck
    foreach ($results as $result) {
    $choices[] = array("value" => $result->value, "text" => $result->value);
    }

    foreach($form["fields"] as &$field){

    if($field["id"] == 1){
    $field["choices"] = $choices;
    }
    }

    return $form;
    }

    Posted 11 years ago on Wednesday June 26, 2013 | Permalink
  9. Please close this topic. I figured it out. This is as simple as it gets and will help you do more advanced selections with GForms. I've seen a few questions for this over the last 2 years with no responses.

    Posted 11 years ago on Wednesday June 26, 2013 | Permalink

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