1

I am using autocomplete from drupal so that users can be guided into finding what they are looking for. For an even better user experience it would be great if autocomplete is returning the results in a listbox. I just started to learn drupal programming (and loving it :) ) but I can't find anything on this particular question.

At this moment when I start typing in a textarea, drupal will show a selection of approx. matches in a pulldown list. How do I get those matches in a html listbox?

I am using the code below for autocompletion

/**
* autocomplete helper
* $string = string for search
*/
function _complete_autocomplete($string) {
  $matches = array();
  $result = db_select('notebooks', 'c')
    ->fields('c', array('product'))
    ->condition('product', '%' . db_like($string) . '%', 'LIKE')
->range(0, 10)
    ->execute();

  // save the query to matches
  foreach ($result as $row) {
    $matches[$row->product] = check_plain($row->product);
  }
  // Return the result to the form in json
  drupal_json_output($matches);
} 

1 Answer 1

1

Okay I got a bit working. But it only fills the select options after the textfield is losing focus. How can I make it so that while typing in the textfield, the select options is filled with data. Kinda similar as to autocomplete?

And another question, if i press enter I get an HTTP error . An Ajax HTTP request terminated abnormally. path: /?=system/ajax status : text responsetext: ready/state:4

Any ideas why this is happing?

<?php

function dropdown_menu(){

$items['test/drop'] = array(
'title' => 'Fill select options from db',
'page callback' => 'drupal_get_form',
'page arguments' => array('select_dropdown'),
'access callback' => TRUE,
'weight' => 4,
'type' => MENU_NORMAL_ITEM,
);

return $items;
}

function dropdown_submit($form, &$form_state) {
//dpm($form);
drupal_set_message('Submit handler was called.');
}

function select_dropdown($form, &$form_state) {

$form['searchfield'] = array(
        '#title' => t('Search database and fill select options'),
        '#type' => 'textfield',
        '#required' => TRUE,
        '#maxlength' => 115,
        '#size' => 55,
        '#weight' => 0,
'#default_value' => 'look for it',
'#ajax' => array(
  'event' => 'change',
  'callback' => 'search_db',
  'wrapper' => 'dropdown-second-replace',
),
    );

  // Get the list of options to populate the first dropdown.
$options_first = _drop_get_first_dropdown_options();
$selected = isset($form_state['values']['searchfield']) ? $form_state['values']['searchfield'] : key($options_first);
$form['dropdown_second'] = array(
'#type' => 'select',
'#title' => 'Options filled from db',
'#prefix' => '<div id="dropdown-second-replace">',
'#suffix' => '</div>',
'#size'=>10,
'#options' => _drop_get_second_dropdown_options($selected),
'#default_value' => isset($form_state['values']['dropdown_second']) ? $form_state['values']['dropdown_second'] : '',
);


//  $form['submit'] = array(
// '#value' => 'Submit',
//   '#type' => 'submit',
// '#submit' => array('dropdown_submit')
// );

  return $form;
}


function drop_dependent_dropdown_callback($form, $form_state) {
return $form['dropdown_second'];
}

function search_db($form, $form_state) {
return $form['dropdown_second']; 
}


// normally pulling data from the database.
function _drop_get_first_dropdown_options() {
return drupal_map_assoc(array(t('empty')));
}

function _drop_get_second_dropdown_options($key = '') {
$matches = array();
$result = db_select('tabel', 'c')
->fields('c', array('product', 'xtra'))
->condition('product', '%' . db_like($key) . '%', 'LIKE')
// ->range(0, 10)
->execute();

// save the query to matches
foreach ($result as $row) {
$matches[$row->product] = check_plain($row->product) . ' xtra:' . ($row->xtra);
}
return($matches);

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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