1

一些我如何设法使其工作。但是,结果并不会与自动完成一起出现。Drupal自定义表单和自动填充问题

现在发布我的最新代码,

文本字段代码

$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); 
} 

我希望这马最后的编辑。

目前的情况,自动完成工作

URL是H找到/镇/自动/ MTW

,但它是不能够从数据库中找到任何数据。我找到了为什么并且无法修复它。 这是因为在上面添加的最后一个函数中,$ string需要是'搜索查询',但它总是将数据库查询为'autocomplete'。我的意思是$ string变量始终具有“自动完成”值,而不是用户输入的值。

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

请请人帮助我..

回答

0
`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> 
+0

我试过了。还是行不通。但我知道这个修复并不是真的对我的问题,因为当我输入我看不到任何请求与文本。自动填充功能对文本字段无效。 –

+0

尝试检查您提供的自动完成功能的路径。 我已经编辑了一个小例子的答案,并为我工作。 –

+0

感谢Ranjeet,尝试了很多解决方案,没有任何工作。 –