How can I do the dynamic select in Drupal 7 forms? I work with drupal 7. In the following code I call the function players() which returns array of values for option. This values were defined beforehand by hand. How can I load it dynamically from the database?

 $form['officers']['officer'] = array( 
'#title' =&gt; t('Select the officer'), 
'#type' =&gt; 'select', 
'#description' =&gt; t('Please, select the officer'), 
'#options' =&gt; players(), 
'#multiple' =&gt; FALSE, 
'#empty_value' =&gt; ' ',
'#empty_option' =&gt; '- None -',  

);


For example in the "Text Field" there is property #autocomplete which defines a path where Drupal’s automatically included JavaScript will send HTTP requests using jQuery.

 $form['team_name'] = array( 
'#title' =&gt; t('Name'), 
'#type' =&gt; 'textfield', 
'#description' =&gt; t('Please, insert your name'), 
'#autocomplete_path' =&gt; 'autofillform/teamname' ,

);


And i created the module which handles it.

&lt;?php
function autofillform_menu() {
$items['autofillform/teamname'] = array(  
'title' =&gt; 'Example autocomplete',
'page callback' =&gt; 'autofillform_teamname',  
'access callback' =&gt; TRUE,
'type' =&gt; MENU_CALLBACK,
 );
 return $items;
 }
 function autofillform_teamname($string){
 $matches = array();
 $result = db_query("SELECT name FROM users where lower(name) like '".$string."%'  LIMIT 10");
 foreach ($result as $user) {
 $matches[$user-&gt;name] = check_plain($user-&gt;name);
  } 
 print drupal_json_encode($matches);
 return; 
 }


Are there any possibility to do the same with "Select" ?
You need to focus on #ajax option. That will look like

$form['officers']['officer'] = array( 
  '#title' =&gt; t('Select the officer'), 
  '#type' =&gt; 'select', 
  '#description' =&gt; t('Please, select the officer'), 
  '#options' =&gt; array(), 
  '#multiple' =&gt; FALSE, 
  '#empty_value' =&gt; ' ',
  '#prefix' =&gt; '&lt;div id="officer-wrapper"&gt;',
  '#suffix' =&gt; '&lt;/div&gt;'
  '#empty_option' =&gt; '- None -',  
  '#ajax' =&gt; array(
     'callback' =&gt; 'path/to/callback/handler',
     'wrapper' =&gt; 'officer-wrapper',
     'method' =&gt; 'replace',
     'effect' =&gt; 'fade',
  ),
);


In the submit handler you will have change the form element for #options and render to post back using drupal_json_output.