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,嘗試了很多解決方案,沒有任何工作。 –