Problem with submitting ajax form with autocomplete I've been a newbie to drupal and trying to add feature to our corporate website (this module 
called 'userpasswords2' searches database of local mailsystem passwords).
I'm using AJAX autocomplete and modification of the form.

AJAX autocomplete works fine, I can choose the user but I have a problem - I cannot submit the form by enter key. If I submit it by hittin 'submit' button by mouse, everything works fine. But I cannot submit it from keyboard.

I would like it to work like here https://api.drupal.org/api/drupal - user enters for example 'hook', chooses what he/she needs, hits enter, then hits enter again and get the result!

So again - clicking submit button works fine, hitting 'enter' key doesn't work.

Googled a lot, the workarounds I've found doesn't work for me. Please help.

function userpasswords2_menu() {
  $items = array();
  $items['userpasswords2'] = array(
      'title' =&gt; 'User passwords',
      'page callback' =&gt; 'drupal_get_form',
      'page arguments' =&gt; array('userpasswords2_form'),
      'access callback' =&gt; TRUE,
      'type' =&gt; MENU_CALLBACK,
  );
  $items['userpasswords2/ajax_username_autocomplete_callback2'] = array(
      'page callback' =&gt; 'ajax_username_autocomplete_callback2',
      'type' =&gt; MENU_CALLBACK,
      'access callback' =&gt; TRUE,
  );
  return $items;
}

function userpasswords2_form($form, &amp;$form_state) {
  $form = array();
  $form['user'] = array(
      '#type' =&gt; 'textfield',
      '#title' =&gt; t('Введите имя пользователя'),
      '#autocomplete_path' =&gt; 'userpasswords2/ajax_username_autocomplete_callback2',
      '#executes_submit_callback' =&gt; 'TRUE',
  );
  $form['box'] = array(
    '#type' =&gt; 'markup',
    '#prefix' =&gt; '&lt;div id="box"&gt;',
    '#suffix' =&gt; '&lt;/div&gt;',
    '#markup' =&gt; '&lt;br&gt;',
  );
  $form['actions']['submit'] = array(
    '#type' =&gt; 'submit',
    '#ajax' =&gt; array(
      'callback' =&gt; 'userpasswords2_callback',
      'wrapper' =&gt; 'box',
    ),
    '#value' =&gt; t('Submit'),
  );
  return $form;


}

function ajax_username_autocomplete_callback2($string = "") {
  $matches = array();
  if ($string) {
    $result = db_select('my_domain_passwords')
      -&gt;fields('my_domain_passwords',array('fullname','password'))
      -&gt;condition('fullname', db_like($string) . '%', 'LIKE')
      -&gt;range(0,10)
      -&gt;execute();
    foreach ($result as $user) {
      $form['custom']['username'] = $matches[$user-&gt;fullname] = check_plain($user-&gt;fullname);
      $form['custom']['password'] = check_plain($user-&gt;password);
    }
  }
  drupal_json_output($matches);
}


function userpasswords2_form($form, &amp;$form_state) {
$form = array();
$form['user'] = array(
    '#type' =&gt; 'textfield',
    '#title' =&gt; t('Введите имя пользователя'),
    '#autocomplete_path' =&gt; 'userpasswords2/ajax_username_autocomplete_callback2',
    '#executes_submit_callback' =&gt; 'TRUE',
);

$form['box'] = array(
  '#type' =&gt; 'markup',
  '#prefix' =&gt; '&lt;div id="box"&gt;',
  '#suffix' =&gt; '&lt;/div&gt;',
  '#markup' =&gt; '&lt;br&gt;',
);
$form['actions']['submit'] = array(
  '#type' =&gt; 'submit',
  '#ajax' =&gt; array(
    'callback' =&gt; 'userpasswords2_callback',
    'wrapper' =&gt; 'box',
  ),
  '#value' =&gt; t('Submit'),
);
return $form;
}

function userpasswords2_callback($form, &amp;$form_state) {
  if ( (!empty($form_state['custom']['username'])) &amp;&amp; (!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;
}
I think you need to do this:

$form['actions']['submit'] = array(
    '#type' =&gt; 'submit',
    '#ajax' =&gt; array(
      'callback' =&gt; 'userpasswords2_callback',
      'wrapper' =&gt; 'box',
      'event' =&gt; 'click'
    ),
    '#value' =&gt; t('Submit'),
  );


just add 'event' => 'click' you your submit ajax element in the form. This must be done for all forms you want to be submitted through #ajax.