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 add title attribute to dropdown

  1. Hi
    In summary I want to include the title attribute in the option of my prepopulated dropdown using the gform_pre_render filter.
    I am using the code provided for the gform_pre_render to prepopulate a dropdown with a query from my database. It works great in passing the value and text attributes but I would also like to include the 'title' attribute in the 'option' but not sure how. I have included it in my $items array but is not getting passed through. Here is my code:

    foreach($races as $race) {
    
    	$items[] = array("value" => $race['title'], "text" =>$race['title'], "title" =>$race['distance']);
    	foreach($form["fields"] as &$field)
    	if($field["id"] == 1){
    	$field["choices"] = $items;
    	}
    	return $form;
    	}
    Posted 12 years ago on Thursday December 1, 2011 | Permalink
  2. The title attribute of the select option isn't currently output by Gravity Forms so passing it when populating the field dynamically isn't going to make it output when it is displayed.

    Currently the only thing utilizes is the value of the option and the text of the option which is what is displayed in the drop down.

    We can look at enhancing the hook to allow this in a future release, however it's not currently a built in option.

    Posted 12 years ago on Thursday December 1, 2011 | Permalink
  3. Ok thanks for the reply! Is there a way then to retrieve the value from a table within wordpress based on the selection of the pre-populated dropdown? ie. query the database based on the value of the dropdown. I guess because it is client side and not server side I need to think of another solution?

    Posted 12 years ago on Thursday December 1, 2011 | Permalink
  4. It's going to depend on what you are trying to do.

    If you want to set the value of a user visible field based on a drop down selection that triggers a query of your WordPress table and sets the value based on the selection, this is client side and would require you to write your own custom jQuery using AJAX to do this.

    If the field you are storing in does not need to be visible on the form you could do this server side. Add a hidden field to your form, or a standard field set to visibility Admin Only, and then use the gform_pre_submission hook to 1) Execute your custom code to query the WordPress data you want to query based on the drop down selection and 2) Store that value in the field you created to contain it.

    Posted 12 years ago on Thursday December 1, 2011 | Permalink
  5. You would have to implement AJAX to be able to pull this off, but it is doable. Another alternative is to use a multi-page form with your drop down on the first page and the dependent field on the second page.

    Posted 12 years ago on Thursday December 1, 2011 | Permalink

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