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 list with usernames

  1. chrishill123
    Member

    ~I would like to be able to populate a drop down box with a list of all usernames
    Possible?

    Posted 7 years ago on Monday November 26, 2012 | Permalink
  2. chrishill123
    Member

    Here's the code I'm working on in functions.php

    function populate_userdrop($form){
    
        //only populating drop down for form id 1
        if($form["id"] != 1)
    
        return $form;
    
        //Creating item array.
        $items = array();
        // Get the custom field values stored in the array
        $meta = get_users();
    
    if (is_array($metas))
    {
    foreach($metas as $meta)  $items[] = array("value" => $meta->display_name, "text" => $meta->display_name);
    }
        //Adding items to field id 1. Replace 1 with your actual field id. You can get the field id by looking at the input name in the markup.
        foreach($form["fields"] as &$field)
            if($field["id"] == 1){
                $field["choices"] = $items;
            }
    
        return $form;
    }

    All I've done on the form is allowed it to be dynamically populated.

    Here's the form in action - http://ethicalincubator.com/BMentor/form-test/

    Posted 7 years ago on Monday November 26, 2012 | Permalink
  3. David Peralty
    Administrator

    Wait, did you figure this out? On your form, I see a dropdown with user names in it.

    Posted 7 years ago on Monday November 26, 2012 | Permalink
  4. chrishill123
    Member

    I AM STOOPID. After debugging I realised I missed the 's' of 'metas'.

    Thanks for the reply. Here is help for anyone else trying this.

    Here's an example of my fully working code to populate a dropdown box in Gravity forms with a list of user display names.

    Add this to functions.php

    // This adds display names for all users to a drop down box on a gravity form.
    add_filter("gform_pre_render", "populate_userdrop");
    
    //Note: when changing drop down values, we also need to use the gform_admin_pre_render so that the right values are displayed when editing the entry.
    add_filter("gform_admin_pre_render", "populate_userdrop");
    
    function populate_userdrop($form){
    
        //only populating drop down for form id 1 - if editing this change to your own form ID
        if($form["id"] != 1)
    
        return $form;
    
        //Creating item array.
        $items = array();
        // Get the custom field values stored in the array
    	// If editing this lookup where you would like to get your data from
    	// this example loads through all users of the website
        $metas = get_users();
    
    if (is_array($metas))
    {
    // in this example we just load the display_name for each user into our drop-down field
    foreach($metas as $meta)  $items[] = array("value" => $meta->display_name, "text" => $meta->display_name);
    }
        //Adding items to field id 1. Replace 1 with your actual field id. You can get the field id by looking at the input name in the markup.
        foreach($form["fields"] as &$field)
            if($field["id"] == 1){
                $field["choices"] = $items;
            }
    
        return $form;
    }
    Posted 7 years ago on Monday November 26, 2012 | Permalink
  5. David Peralty
    Administrator

    Yeah, I saw your if statement and was like "where did he define metas?" Glad you got it sorted.

    Posted 7 years ago on Monday November 26, 2012 | Permalink
  6. Thank you chrishill123, this exactly what i was looking for!

    Posted 6 years ago on Saturday May 4, 2013 | Permalink

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