I have this code:
function THEME_form_user_login_alter (&$form, &$form_state){
$form['actions']['submit']['#ajax'] = array(
'callback' => 'ajax_process_form',
'wrapper' => 'user-login',
'effect' => 'fade',
'speed' => 'fast',
'method' => 'replace'
);
};
It work when i click on the submit button, but when i press ENTER Ajax doesn't get triggered.. Is there a way around that?
Edit: I tried to enable ajax on all fields and set keystroke to True, but then only one field is sent so i get an error as the form process script is expecting two fields.
I realize this is an old thread but it shows up high in the Google search results.
Just set your submit button's Ajax event to 'click'. I believe its default value is actually 'mousedown' which won't make it trigger when the enter key is pressed within a field of the form.
If you set it to 'click', that event will trigger when the submit button is clicked as well as when the enter button is pressed within a form field.
Just make sure you explicitly set ajax event to click and it'll work:
$form['actions']['submit']['#ajax'] = array(
'callback' => 'ajax_process_form',
'wrapper' => 'user-login',
'effect' => 'fade',
'speed' => 'fast',
'method' => 'replace',
// This is what you need:
'event' => 'click'
);
I haven't tried this, but you should be able to set the event on ajax to include keydown:
function THEME_form_user_login_alter (&$form, &$form_state){
$form['actions']['submit']['#ajax'] = array(
'callback' => 'ajax_process_form',
'wrapper' => 'user-login',
'effect' => 'fade',
'speed' => 'fast',
'method' => 'replace'
'event' => array('click', 'keydown'),
);
}
I'm not sure how well keydown will work on a submit button, usually you want something more complex that makes sure that the matched key is Enter. Alternatively you could catch the Enter submit yourself and prevent default (form submit) and the click the button yourself. That would trigger the ajax call like it was clicked and would keep the Drupal powered AJAX intact, which makes things easier to handle server side.