I am creating the select box form element as following

$form['select-user-group'] = array(
       '#type' => 'select',
       '#title' => t('Select a group'),
       '#options' => select_user_dropdown_options(),      
       '#description' => t('Select a group to send the mail.'))

and am trying load the options for select box like follwing

function select_user_dropdown_options(){
    $result = db_query("select * from {content_type_usergroup}");   
    $record = db_fetch_object($result);
    $str = '<select><option>-- Select a user group--</option>';
    while ($obj = db_fetch_object($result)) {
        $str .= '<option>'+$obj->field_p_group_name_value+'</option>';
     $str .='</select>';
     return $str;

But its not loading options to the select box , please help


Your select_user_dropdown_options() function needs to return an array of options, not a block of HTML. See the #options section of the Form API documentation -- you should find yourself re-reading that Form API page a lot.

Your array will be in the form

  'group_1' => 'Group 1',
  'group_2' => 'Group 2',

A pedantic note -- I believe that's really a function call as opposed to a callback. That's fine; I mention it mostly because you don't include the parentheses when providing the names of callback functions.

  • thank you , but am getting like options load like along with optgroup – KTM Jul 15 '15 at 15:58
  • now am getting it like this : <select name="select-user" class="form-select" id="edit-select-user"><optgroup label="0"><option value="0">-- Select a user --</option></optgroup><optgroup label="1"><option value="0">vmadmin</option></optgroup><optgroup label="2"><option value="0">auto</option></optgroup><optgroup label="3"><option value="0">mnc</option></optgroup></select> how can i remove opt group ? – KTM Jul 15 '15 at 16:17
  • Did my answer address your original question or is that still unresolved? – Michael Greisman Jul 29 '15 at 22:36
  • Its actually solved some how , i was returning a string insted of array , thanks – KTM Jul 30 '15 at 13:18
  • Yeah, returning an array -- in that form -- was crucial. The Form API page gives a lot more detail and is very helpful. Would you mind marking this as a successful answer, please? – Michael Greisman Aug 11 '15 at 19:45

The call back function should be like follows :

function select_user_dropdown_options(){
    $result = db_query('select * from {users}');
    $options = array();
    $options[] = '-- Select a user  --';
    while ($row = db_fetch_object($result)) {
      $options[$row->uid] = $row->name;
     return $options;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.