I am trying to create a form with a auto-complete textfield for a particular role type usersname. But it's not working. Though when I use "user/autocomplete" which is default path given in example over drupal.org it works...
This is for hook_menu ---
function scout_bucks_menu(){
$items = array();
$items['scouts/autocomplete'] = array(
'page callback' => 'scout_bucks_autocomplete',
'type' => MENU_CALLBACK,
'access arguments' => array('administer scout bucks'),
);
return $items;
}
This id for form --
function scoutbucks_add_bucks_form(&$form_state){
$form = array();
$form['scout_name'] = array(
'#type' => 'textfield',
'#title' => t('Scout Name'),
'#maxlength' => 60,
'#autocomplete_path' => 'scouts/autocomplete',
'#reguired' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#title' => t('ADD BUCKS TO SCOUT PROFILE'),
'#prefix' => '<div class="submit-button-bucks">',
'#suffix' => '</div>',
);
return $form;
}
This is the function for auto-complete ---
function scout_bucks_autocomplete($string = '') {
$matches = array();
$userobj = db_fetch_object(db_query("SELECT users.name, users.uid FROM {users}, {users_roles} WHERE users.uid = users_roles.uid AND users_roles.rid = 7"));
while ($row = db_fetch_array($userobj)) {
$matches[$row['users.uid']] = $row['users.name'];
}
drupal_json($matches);
}
What can be the reason ?
You need to change drupal_json
to drupal_to_js .
function scout_bucks_autocomplete($string = '') {
$matches = array();
$userobj = db_fetch_object(db_query("SELECT users.name, users.uid FROM {users}, {users_roles} WHERE users.uid = users_roles.uid AND users_roles.rid = 7"));
while ($row = db_fetch_array($userobj)) {
$matches[$row['users.uid']] = $row['users.name'];
}
drupal_to_js($matches); // the edited line.
}
Hope this works...
Sample from working autocomplete on one of my sites: function module_menu() {
$items['app/autocomplete/%'] = array(
'title' => 'Some title',
'description' => '',
'page callback' => '_module_autocomplete',
'page arguments' => array(2),
'type' => MENU_CALLBACK,
);
}
function _module_autocomplete($param) {
$result = db_query('SELECT id,stuff FROM {table} WHERE a=%d', $param);
while ($row = db_fetch_object($result)) {
$ret[$row->id] = $row->stuff;
}
drupal_json($ret);
}
I've cut down as much as I feel safe to, but I do think it is bestif you define that % in the menu callback.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.