Autocomplete Drupal自定义表单和自动完成问题

Autocomplete Drupal自定义表单和自动完成问题,autocomplete,drupal-7,drupal-modules,drupal-theming,drupal-forms,Autocomplete,Drupal 7,Drupal Modules,Drupal Theming,Drupal Forms,我是如何设法使它工作的。但结果仍然不是自动完成的 现在发布我的最新代码 文本字段代码 $form['town'] = array( '#type' => 'textfield', '#required' => TRUE, '#autocomplete_path' => 'hfind/town/autocomplete', ); 菜单功能代码 function hfind_menu() { $items = array(); $items['hfind/town/aut

我是如何设法使它工作的。但结果仍然不是自动完成的

现在发布我的最新代码

文本字段代码

$form['town'] = array(
'#type' => 'textfield',
'#required' => TRUE,
'#autocomplete_path' => 'hfind/town/autocomplete',
);
菜单功能代码

function hfind_menu() {
  $items = array();
  $items['hfind/town/autocomplete'] = array (
    'title' => 'Autocomplete for cities',
    'page callback' => 'hfind_town_autocomplete',
    'access arguments' => array('use autocomplete'),
    'type' => MENU_CALLBACK
   );
return $items;
}
function hfind_town_autocomplete($string){
  $matches = array();
  $result = db_select('towns', 't')
    ->fields('t', array('town_name'))
    ->condition('town_name', '%' . db_like($string) . '%', 'LIKE')
    ->execute();
  foreach ($result as $row) {
    $matches[$row->city] = check_plain($row->city);
  }
  drupal_json_output($matches);
}
回调函数代码

function hfind_menu() {
  $items = array();
  $items['hfind/town/autocomplete'] = array (
    'title' => 'Autocomplete for cities',
    'page callback' => 'hfind_town_autocomplete',
    'access arguments' => array('use autocomplete'),
    'type' => MENU_CALLBACK
   );
return $items;
}
function hfind_town_autocomplete($string){
  $matches = array();
  $result = db_select('towns', 't')
    ->fields('t', array('town_name'))
    ->condition('town_name', '%' . db_like($string) . '%', 'LIKE')
    ->execute();
  foreach ($result as $row) {
    $matches[$row->city] = check_plain($row->city);
  }
  drupal_json_output($matches);
}
我希望这可能是最后的编辑

目前的情况是,自动完成正在发挥作用

url为hfind/town/autocomplete/mtw

但它无法从数据库中找到任何数据。我发现了原因,但无法修复它。 这是因为在我上面添加的最后一个函数中,$string需要是“search query”,但它总是以“autocomplete”的形式查询数据库。我的意思是$string变量始终具有“autocomplete”值,而不是用户键入的值

还有一个问题是,即使向所有类型的用户提供了访问表单上搜索自动完成的权限,来宾用户也无法使用该功能


请有人帮帮我。

我试过了。仍然不起作用。但我知道这个修正并不是针对我的问题,因为当我键入时,我看不到任何带有文本的请求。文本字段的自动完成功能未激活。请尝试检查您提供的自动完成功能的路径。我用一个小例子编辑了我的答案,这对我来说很有用。谢谢Ranjeet,尝试了很多解决方案,但都不起作用。你能展示你最近尝试的自动完成代码吗。我这样问是因为我发布的代码非常简单,而且总是对我有用。
`drupal_json_output()` instead of `drupal_to_js` and remove `print` .
<code>
hook_menu() {
$items['cnetusers/autocomplete'] = array(
    'title' => 'Auto complete path',
    'page callback' => 'cnetusers_employees_autocomplete',
    'page arguments' => array(2, 3, 4, 5),
    'access arguments' => array('access user profiles'),
    'type' => MENU_CALLBACK,
  );
return $item;
}
// my autocomplete function is like this
function cnetusers_employees_autocomplete() {
  // write your sql query
    $matches["$record->ename $record->elname [id: $record->uid]"] = $value;
  }
  if (empty($matches)) {
    $matches[''] = t('No matching records found.');
  }
  drupal_json_output($matches);
}

$form['disc_info']['approval'] = array(
      '#type' => 'textfield',
      '#title' => t('Approval By'),
      '#autocomplete_path' => 'cnetusers/autocomplete',
    );
</code>