Hello,
At the moment i'm writing a module for Drupal 6 where I wanted to use the autocomplete function.
I don't know what i'm doing wrong but i always get an error ( "an HTTP error 403 occurred") when I type something in my textfield. (When i use a standard autocomplete path like 'user/autocomplete', there are no problems).

Is there somebody who knows a solution?

The code that i use...

in function test_menu()

       $items['test/autocomplete'] = array(
      'title' => 'Autocomplete',
      'callback' => 'test_autocomplete',
      'acces'=> TRUE,
      'access arguments' => array('access content'),
      'type' => MENU_CALLBACK,
      'file' => 'test.inc',
      );

in form function

    $form['plaats'] = array(
    '#type' => 'textfield',
    '#title' => t('plaats'),
    '#size' => 30,
    '#autocomplete_path' => 'test/autocomplete',
    '#maxlength' => 64,
    '#tree' => TRUE,
  );

in test.inc

function test_autocomplete($fieldname) {
  $matches = array();
    $result = db_query_range("SELECT name FROM {plaatsen} WHERE LOWER(name) LIKE LOWER('%%s%%')", $string, 0, 10);
  // found wrote into $matches
  while ($data = db_fetch_object($result)) {
    $matches[$data->name] = check_plain($data->name);
  }
  // return for JS
  drupal_json($matches);
}

Comments

klaasm’s picture

Ok, i found the solution myself. I had to use "page callback" and "access callback".

This is the corrected code

$items['test/autocomplete'] = array(
    'title' => 'Autocomplete',
    'page callback' => 'test_autocomplete',
    'access callback' => TRUE,
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
    'file' => 'test.inc',
  );

There was also a little error in test.inc . ( LOWER('%s%%') )
corrected code :


function treinen_autocomplete($string = '') {
  $matches = array();
  if ($string) {
    $result = db_query_range("SELECT name FROM {plaatsen} WHERE LOWER(name) LIKE LOWER('%s%%')", $string, 0, 10);
    while ($data = db_fetch_object($result)) {
      $matches[$data->name] = check_plain($data->name);
    }
  }

  drupal_json($matches);
}


netron’s picture

in your test_menu() function, how do you pass $items in to it?
do you return any values?

greg911’s picture

I've tried you code and it's not working for me... can you please post a zip of your test module ? I'me currently trying to add autocomplete functionnality to my module but without any success....

Thanks !!

klaasm’s picture

Do you have still problems with it? Normally it should work...

gayatri.sa’s picture

Your ''access callback' => TRUE,' tip really saved the day for me....thank you!
Though I wonder why is it mandatory?

FYI, I was trying to get the auto-complete feature of the custom profile field (D6) to work.
When I added the access callback all work fine.