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' => 'select',
'#title' => t('Select a group'),
'#options' => array(
'' => t('-- Select a user group --'),
),
'#ajax' => array(
'event' => 'load',
'callback' => 'selectuser_ajax_callback',
'method' => '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 = '<select>';
while ($obj = db_fetch_object($result)) {
$str .= '<option value=' + $obj->vid + '>' + $obj->field_p_group_name_value + '</option>';
}
$str .= '</select>';
echo $str;
}
But it's not working. How to make this function work properly to populate the select list '#options
?