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.

Dynamically Populating a Custom Field Drop Down

  1. marcusk
    Member

    Hi, I have extended user registration fields using Cimmy User Extra Fields plugin and added a dropdown box of a list of companies.

    What I need to do is to insert the same field into a GF form by pre populating the data from the wp_cimy_uef_fields MySQL table where Cimmy plugin has stored the information.

    Been searching forum and documentation but can't seem to find a solution. Is this possible and how do I go about doing it if yes.

    Posted 6 years ago on Monday September 10, 2012 | Permalink
  2. David Peralty
    Administrator

    Hi marcusk, you'll be wanting to look at gform_pre_render:

    http://www.gravityhelp.com/documentation/page/Gform_pre_render

    Posted 6 years ago on Monday September 10, 2012 | Permalink
  3. marcusk
    Member

    Hi, I have the drop down values in an array (Company A, Company B, Company C, Others). I tried to make use of the gform_pre_render but I'm not really good at it. The result I got was a drop down list with only 1 item ie Company A, Company B, Company C, Others instead of it being 4 items in the list. Please help. (Source code below)

    function populate_dropdown($form){
    
      $con = mysql_connect("localhost","root","");
      if (!$con)
        {
        die('Could not connect: ' . mysql_error());
        }
    
      mysql_select_db("asm", $con);
    
      $result = mysql_query("SELECT * FROM <code>wp_cimy_uef_fields</code> WHERE ID = '1'");
      $label = mysql_fetch_assoc($result);
      $company = array();
      $company = $label[LABEL];
    
      $items = array();
      $items[] = array("text" => "", "value" => "");
    
      $items[] = array("text" => $company, "value" => $company);
    
      foreach($form["fields"] as &$field)
            if($field["id"] == 18){
                $field["choices"] = $items;
            }
      return $form;
    Posted 6 years ago on Wednesday September 12, 2012 | Permalink
  4. marcusk
    Member

    Tried an alternative method by doing predefined choices:
    http://www.gravityhelp.com/documentation/page/Gform_predefined_choices

    I'm still getting the same problem however. Source code below:

    add_filter("gform_predefined_choices", "add_predefined_choice");
    
    function add_predefined_choice($choices){
    
    	$con = mysql_connect("localhost","root","");
    	if (!$con)
      	{
      	die('Could not connect: ' . mysql_error());
      	}
    
    	mysql_select_db("asm", $con);
    
    	$result = mysql_query("SELECT * FROM <code>wp_cimy_uef_fields</code> WHERE ID = '1'");
    	$label = mysql_fetch_assoc($result);
    	$company = $label[VALUE];
    
    	$choices["Company List"] = array($company);
    	return $choices;
    
    }

    Please help

    Posted 6 years ago on Thursday September 13, 2012 | Permalink
  5. This is a customization beyond the support we can provide here. Sounds like you need help from someone versed in PHP or WordPress to help you with this. You can find some resources on our job board: http://www.gravityhelp.com/forums/forum/job-board

    Posted 6 years ago on Friday September 14, 2012 | Permalink