How can I dynamically load options into a select list? I'm creating form using drupal_get_form and I'm using the following code for creating a select list:

$form['select-user-group'] = array(
  '#type' =&gt; 'select',
  '#title' =&gt; t('Select a group'),
  '#options' =&gt; array(
    '' =&gt; t('-- Select a user group --'),        
  ),
  '#ajax' =&gt; array(
  'event' =&gt; 'load',
  'callback' =&gt; 'selectuser_ajax_callback',
  'method' =&gt; 'replace',
);


and then I'm trying to define a function like following to dynamically add options to the select list:

function selectuser_ajax_callback() {

  db_set_active('drupal');

  $result = db_query("select * from {content_type_usergroup}");
  $record = db_fetch_object($result);

  $str = '&lt;select&gt;';
  while ($obj = db_fetch_object($result)) {
    $str .= '&lt;option value=' + $obj-&gt;vid + '&gt;' + $obj-&gt;field_p_group_name_value + '&lt;/option&gt;';
  }
  $str .= '&lt;/select&gt;';

  echo $str;
}


But it's not working. How to make this function work properly to populate the select list '#options?
For just populating select list options, you don't need to go through an Ajax approach! Simply write a separate function and write your database query inside it and then just call that function in '#options' of your $form['select-user-group'].

For example for displaying all node types in a select list you can use the following function:

function get_node_type_info() {

  $wizard_plugins = node_type_get_types();

  $options = array();

  foreach ($wizard_plugins as $key =&gt; $wizard) {
    // Don't forget the $ before wizard
    $options[$key] = $wizard-&gt;name;
  }

  return $options;
}


And then use this function to populate the '#options' like so:

$form['display_options'] = array(
  '#type' =&gt; 'select',
  '#title' =&gt; t('Component Type'),
  '#options' =&gt; get_node_type_info(),
);