Hello,

I'm currently building an autocomplete text field with a twist - it will be querying an OpenX database in order to retrieve a list of previous advertisers or agencies

i have a "netron.module" and here's the relevant code

<?php
//  hook_menu implementation
function menu_menu()
{
$items["netron/autocomplete"] = array(
'title' => 'Autocomplete for agencies',
'page_callback' => 'netron_agency_autocomplete',
'access_callback' =>TRUE,
'access arguments' => array('access content'),
'type' => MENU_CALLBACK
  
);
}
//  implement hook_form_alter.  turns a text field into an autocomplete text field
function netron_form_alter(&$form, $form_state, $form_id)
{
if (
$form_id == "netron_node_form")
{
   
$form['field_agency']=array(
   
'#type'=>'textfield',
   
'#title'=>t('Agency'),
   
'#size'=>30,
   
'#maxlength'=>64,
   
'#autocomplete_path' =>'netron/autocomplete'
   
);
}
// retrieves an array of agency names from an openx database
function netron_agency_autocomplete()
{
/* openx db connection stuff here... your mileage may vary  */
$matches = array();
$result = mysql_query("SELECT name FROM ox_agency where LOWER(name) LIKE LOWER('%s%%')");
    while (
$row=mysql_fetch_object($result) )
    {
   
$matches[$row->name] = check_plain($row->name);
    }
  print
drupal_to_js($matches);
}
?>

the problem is that the agency field on netron_node_form does turn into an autocomplete - but throws an error ,as it can't find the path
netron/autocomplete

so its not even getting to doing a function callback. is there something that i'm missing from the above code?

Comments

function menu_menu() is a typo

it's actually

function netron_menu()

new version of hook_menu : note that there are now no underscores in "page callback" or "access callback"
added return $items;

function netron_menu()
{
$items["netron/autocomplete"] = array(
'title' => 'Autocomplete for agencies',
'page callback' => 'netron_agency_autocomplete',
'access callback' =>TRUE,
'access arguments' => array('access content'),
'type' => MENU_CALLBACK
);
return $items;
}

got this working.

had to visit
admin/build/modules

then back to my module form - and it worked.

however the autocomplete is returning all agency names, so the SQL query needs something passed into it...

altered autocomplete dbase function:

function netron_agency_autocomplete($field_agency='')
{
mysql_pconnect("host","user","pwd");
mysql_query("USE openxbeta");
$matches = array();
if($field_agency) {
$result = mysql_query("SELECT name FROM ox_agency where LOWER(name) LIKE LOWER('%$field_agency%')");
while ($row=mysql_fetch_object($result) )
{
$matches[$row->name] = check_plain($row->name);

}
}
drupal_json($matches);
exit;
}

autocomplete on a text field now works flawlessly.

I finally figure out what went wrong (thanks to your code netron !)

My autocomplete function receive 2 parameters, the first one is "autocomplete" (and stays so, whatever I do!) and the second one is the "textfield" value on which I bound the autocomplete (the $field_agency in netron's case).

I don't really know why it behave like that... maybe because the path is mySearch/autocomplete so the "autocomplete" part is handled by Drupal as a the first "page parameter"... anyways, if it can help someone figure out why it is always searching for the "autocomplete" keyword, this may be your answer :-)

Yea, admin/build/modules worked for me too. Thanks for that tip... I was going out of my mind working on a solution.

__________
Regards,
Chad Hester

<?php
      $form
['field_name'][0]['#autocomplete_path'] = 'oldversion/ac_name';
?>

I had to add the 0 index for my Drupal 6.6 with CCK 2.0 install. Maybe you guys are using CCK 1.x?

j0rd, could you paste the final code for autocomplete fields that works in Drupal 6 and CCK2?

Thanks

Hi,

I use this in my one project and I am very thankful to all of you for such a nice help....!

below is my form and the autocomplete function, it is just not working in drupal6...plz teme the error....
function recipe_search_menu() {

$items = array();
$items["recipe_search/autocomplete"] = array(
'title' => 'recipe search category autocomplete',
'page_callback' => 'recipe_search_autocomplete',
'access_callback' =>TRUE,
'access arguments' => array('access content'),
'type' => MENU_CALLBACK
);

return $items;
}
//////////////////////////////
function recipe_search_form(){

$form = array();

$form['recipe_search']['title'] = array(
'#title' => t('recipe title'),
'#type' => 'textfield',

);
$form['recipe_search']['category'] = array(
'#title' => t('Category'),
'#type' => 'textfield',
'#autocomplete_path' => 'recipe_search/autocomplete',

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

);

return $form;
}
//////////////////////
function recipe_search_autocomplete() {
$matches = array();
$result = mysql_query("SELECT * FROM {term_data} WHERE vid=2");
while ( $row=mysql_fetch_object($result) )
{
$matches[$row->name] = check_plain($row->name);
}
print drupal_to_js($matches);
exit();

}

Since ashiali posted her query about a year ago, she must most definitely have figured out a solution. I'm still posting here since it might help others who stumble upon this post -

The following lines

'page_callback' => 'recipe_search_autocomplete',
'access_callback' =>TRUE,

should be as follows (notice the removal of underscores in page_callback and access_callback) -

'page callback' => 'recipe_search_autocomplete',
'access callback' =>TRUE,

A similar post/query - http://drupal.org/node/740974

Following errors i can see

1> Menu name function is not correct it should be netron_menu()
2> In autocomplete function at last use only drupal_jason($matches) nor print drupal_jason($matches)
3> String is not passed to autocomplete function how u will search?

:)