0

Consider I am doing a registration form with the following fields.

1. Name

2. Email

3. City

Here the city should be auto complete.

I created the city field with the help of content profile module in order to add more fields in future.

Now how can i make the "City" text box auto complete.

How can i store the records in the table ?

Thanks in advance...

2 Answers 2

1

There's a full example for Drupal 6 here: [Textfield that uses autocomplete][1]. The code will probably be useful to people on this site as well so I'll post i here:

The following is based on a module with the name: 'cities'

In your form function:

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

In your module file:

function cities_menu(){
  // path with autocomplete function for cities
  $items['cities/autocomplete'] = array(
    'title' => 'Autocomplete for cities',
    'page callback' => '_cities_autocomplete',
    'access arguments' => array('use autocomplete'),  //or whatever permission makes sense
    'type' => MENU_CALLBACK
  );
  return $items;
}

Also in your module file:

function _cities_autocomplete($string) {
  $matches = array();
  //search table `cities` for cities that begin with the letters the user enters in the form
  $result = db_query_range("SELECT city FROM {cities} WHERE LOWER(city) LIKE LOWER('%s%')", $string, 0, 10);
  // add matches to $matches
  while ($data = db_fetch_object($result)) {
    $matches[$data->city] = check_plain($data->city);
  }
 print drupal_to_js($matches);

}
0

Try Autocomplete Widgets for Text and Number Fields, I use it for this purpose and it works great.

This module adds 3 autocomplete widgets for CCK fields of type Text and Number.

  • Autocomplete for allowed values list: ... snip ...
  • Autocomplete for existing field data: This widget can be used for Text only and it takes candidate values from existing values in the database for that field.
  • (7.x only) Autocomplete for predefined suggestions: ... snip ...

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.