我試圖將功能添加到我們的公司網站(該模塊稱爲「userpasswords2」搜索本地郵件系統密碼數據庫)。我正在使用AJAX自動完成和表單修改。無法通過輸入密鑰提交ajax自動完成表單
我無法通過輸入鍵提交表單。 AJAX自動完成工作正常,但是當我選擇用戶時,表單只能通過提交按鈕提交。
我希望它能像這裏一樣工作https://api.drupal.org/api/drupal - 用戶輸入例如'hook',例如選擇hook_menu,點擊輸入,然後再次輸入並得到結果!
所以再次 - 點擊提交按鈕正常工作,按'輸入'鍵不起作用。
谷歌搜索了很多,我找到的解決方法不適用於我。請幫忙。
function userpasswords2_menu() {
$items = array();
$items['userpasswords2'] = array(
'title' => 'User passwords',
'page callback' => 'drupal_get_form',
'page arguments' => array('userpasswords2_form'),
'access callback' => TRUE,
'type' => MENU_CALLBACK,
);
$items['userpasswords2/ajax_username_autocomplete_callback2'] = array(
'page callback' => 'ajax_username_autocomplete_callback2',
'type' => MENU_CALLBACK,
'access callback' => TRUE,
);
return $items;
}
function userpasswords2_form($form, &$form_state) {
$form = array();
$form['user'] = array(
'#type' => 'textfield',
'#title' => t('Enter username'),
'#autocomplete_path' => 'userpasswords2/ajax_username_autocomplete_callback2',
'#executes_submit_callback' => 'TRUE',
);
$form['box'] = array(
'#type' => 'markup',
'#prefix' => '<div id="box">',
'#suffix' => '</div>',
'#markup' => '<br>',
);
$form['actions']['submit'] = array(
'#type' => 'submit',
'#ajax' => array(
'callback' => 'userpasswords2_callback',
'wrapper' => 'box',
),
'#value' => t('Submit'),
);
return $form;
}
function ajax_username_autocomplete_callback2($string = "") {
$matches = array();
if ($string) {
$result = db_select('my_domain_passwords')
->fields('my_domain_passwords',array('fullname','password'))
->condition('fullname', db_like($string) . '%', 'LIKE')
->range(0,10)
->execute();
foreach ($result as $user) {
$form['custom']['username'] = $matches[$user->fullname] = check_plain($user->fullname);
$form['custom']['password'] = check_plain($user->password);
}
}
drupal_json_output($matches);
}
function userpasswords2_form_validate($form, &$form_state) {
$username = $form_state['values']['user'];
$matches = array();
// select from database by fullname
$result = db_select('my_domain_passwords')
->fields('my_domain_passwords', array('fullname'))
->condition('fullname', db_like($username), 'LIKE')
->range(0,1)
->execute()
->fetchField();
if (!empty($username)) {
$form_state['custom']['username'] = $result;
$password = db_select('my_domain_passwords')
->fields('my_domain_passwords', array('password'))
->condition('fullname', db_like($username), 'LIKE')
->range(0,1)
->execute()
->fetchField();
$form_state['custom']['password'] = $password;
}
return $form;
}
function userpasswords2_callback($form, &$form_state) {
if ((!empty($form_state['custom']['username'])) && (!empty($form_state['custom']['password']))) {
$output_string = $form_state['custom']['username'] . " : " . $form_state['custom']['password'];
} else {
$output_string = "No such user: " . $form_state['values']['user'];
}
$username = $form_state['custom']['username'];
$password = $form_state['custom']['password'];
$element = $form['box'];
$element['#markup'] = $output_string;
return $element;
}
我不知道你爲什麼向我們展示的PHP代碼。你的問題存在於JavaScript中。你使用jQuery或純JavaScript或任何其他庫? – Colandus
@Colandus這是Drupal 7網站,它使用jQuery自動完成據我所知,至少我已經找到'autocomplete.js' – Vlad
那麼,在這種情況下提供的代碼對我們來說絕對沒用,你需要添加一個'onKeypress'事件到你的輸入文字去,並檢查他們是否按回車鍵以編程方式點擊提交按鈕。 – Colandus