I am trying to create a module to register for an online course. It involves the students to input their name, address, email address and name of the school. Currently they are just text fields using the forms api. The problem is that a lot many a times, students from the same school enter the name of school in different manner and as such, one school is stored in the database under multiple names. This becomes an issue if i try to search for a school as not all the entries would come up.

I was thinking if it was possible to have an autocomplete text box similar to the autocomplete used in live search in drupal and the search box in browsers (top right) ..

has this been done by anyone ? can anyone help me out with this ??

thanks a lot. your help is well appreciated.


havran’s picture

It's simple with form API. You must have set autocomplete path with autocomplete function callback in your hook_menu() .

    // path with autocomplete function for cities
    $items[] = array(
      'path' => 'cities/autocomplete',
      'title' => 'Autocomplete for cities',
      'callback' => '_cities_autocomplete',
      'access' => TRUE,
      'type' => MENU_CALLBACK

And in your form:

  $form['city'] = array(
    '#type' => 'textfield',
    '#title' => 'City',
    '#maxlength' => 128,
    '#autocomplete_path' => 'cities/autocomplete',

And autocomplete function which search city in table cities looks like:

 * autocomplete helper
 * $string = string for search
function _cities_autocomplete($string) {
  $matches = array();
  // searching in database, only city column
  $result = db_query_range("SELECT city FROM {cities} WHERE LOWER(city) LIKE LOWER('%s%%')", $string, 0, 10);
  // found wrote into $matches
  while ($data = db_fetch_object($result)) {
    $matches[$data->city] = check_plain($data->city);
  // return for JS
  print drupal_to_js($matches);
  // we don't need goto next PHP

Something looks like this working for me (Drupal 5 offcourse). Here is full article but only in Slovak language (sorry) :). http://www.fem.uniag.sk/havran/blog/ako-v-drupale-na-funkciu-autocomplete

My blog - Havran's mini-blog
My first drupal site - http://www.enigma.sk/

My site - Svoji.SK
My first Drupal site - http://www.enigma.sk/

ardnet’s picture

Yes, it does so simple.

thank you so much.

bharanikumariyerphp’s picture

Hi Dear

Thanks for your code snippet,

My doubt is totally different , here is my doubt

I know how to create the custom blocks,

I have followed the pro-drupal development pdf , for creating custom block,

when we create the custom module at end of block module we have to call the theme function for display the block,

for that , in the pdf tutorial , they used item-list function,

so , i make another copy of the item_list function ,(which is located in the theme.inc)

then i simply changed name of the item_list function to myblock_itme_list ,

But i am not sure my way is correct,

Here i need your help, can u please give me your custom block snippet,

It is very much useful for my development,


dhovey’s picture

I have a textfield that is '#size' => 5, and the value to enter is a five character value. I use the autocomplete to bring back a list of values and the related description (e.g. 10000 - General Fund). However, the autocomplete field is displaying the same width as the textfield. How can I make the autocomplete field wider without making the textfield wider?

I have played with the css to little avail, although I did notice that when I placed the following, the list element was wider, but the text and background did not fill the list element's width.

#autocomplete li {
  width: 180px;
  text-align: left;
veeraprasadd’s picture

Veera Prasad Dagudu