0

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' => t('Select the officer'), 
'#type' => 'select', 
'#description' => t('Please, select the officer'), 
'#options' => players(), 
'#multiple' => FALSE, 
'#empty_value' => ' ',
'#empty_option' => '- 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' => t('Name'), 
'#type' => 'textfield', 
'#description' => t('Please, insert your name'), 
'#autocomplete_path' => 'autofillform/teamname' ,

); And i created the module which handles it.

<?php
function autofillform_menu() {
$items['autofillform/teamname'] = array(  
'title' => 'Example autocomplete',
'page callback' => 'autofillform_teamname',  
'access callback' => TRUE,
'type' => 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->name] = check_plain($user->name);
  } 
 print drupal_json_encode($matches);
 return; 
 }

Are there any possibility to do the same with "Select" ?

3
  • Do you want to load them dinamicazlly with ajax or you want to load them from the db in php? – Nicola Peluchetti Jul 20 '11 at 15:22
  • I want to do it in the same manner as I did for "Text field" I did it using php and then converted variables using drupal_json_encode($matches); – alexey Jul 20 '11 at 15:50
  • I edited the post. I added the module to show how i did it with TextField. – alexey Jul 20 '11 at 16:10
1

In any case take a look at the first answer of this question: i think it's what you are lloking for https://stackoverflow.com/questions/5158744/drupal-7-best-practice-for-a-dynamic-select-list-in-drupal-7

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.