How do autocomplete text fields work?
Working with Drupal autocomplete form is good, instead of using the jQuery autocomplete.

Here we have a solution that how to create a custom autocomplete form and to get the user names or whatever.

I am giving the simple example of using it.

Take a menu item in hook_menu to get the autocomplete form. This will call the function called test_autocomplete_form.

$items['autocomplete_form'] = array(
        'title' =&gt; 'Test autocomplete form',
        'page callback' =&gt; 'drupal_get_form',
        'page arguments' =&gt; array('test_autocomplete_form'),
        'access arguments' =&gt; array('access test autocomplete'),
        'type' =&gt; MENU_NORMAL_ITEM
);

// Which will get the autocomplete form
function test_autocomplete_form(){
    $form = array();
    $form['testauto'] = array(
        '#type' =&gt; 'textfield',
        '#autocomplete_path' =&gt; 'test/autocomplete',
        '#description' =&gt; t('Please type any letter.'),
    );
    return $form;
}


We have to mention our custom path in for the #autocomplete_path attribute. Here I have given a path like test/autocomplete which will have to get the autocomplete result for that need to have one more menu item with the same path which we have given for the #autocomplete_path:

// menu item to get the autocomplete result,Which will call the function called 'test_autocomplete'
$items['test/autocomplete'] = array(
        'title' =&gt; 'Test autocomplete',
        'page callback' =&gt; 'test_autocomplete',
        'access arguments' =&gt; array('access test autocomplete'),
        'type' =&gt; MENU_CALLBACK
);

// function to get the autocomplete result, I am taking the user table to get the users names in the autocomplete field
    function test_autocomplete($string = '') {
      $matches = array();
      if ($string) {
        $result = db_query_range("SELECT name FROM {users} WHERE LOWER(name) LIKE LOWER('%s%%')", $string, 0, 10); // only 10 results will show
        while ($user = db_fetch_object($result)) {
          $matches[$user-&gt;name] = check_plain($user-&gt;name);
        }
      }
      drupal_json_output($matches); // Returns the data in JSON format
    }