I've got a form that provides users with several buttons for various options. If the user keys Enter in one of the input textfields, I'd like that to trigger just one button, the main Submit button for the module. My problem is that "Enter" is triggering other buttons with submit functions on the module, and not the main one I need.

Any suggestions on a simple method (Drupal 6.x) for ensuring that "Enter" engages one specific submit fork of several?

Thanks, Dave

Comments

You could try attaching JavaScript/jQuery to your form. On pressing the enter key you could check which form elements contain data and trigger the appropriate event.

I had this issue last year in a custom module I was working on. The method I used to solve this included:

In the form function, I defined three keys as follows:

  // Define a submit button
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save'),
      '#executes_submit_callback' => TRUE,
    );
  // Define a delete button
    $form['delete'] = array(
      '#type' => 'button',
      '#value' => t('Delete'),
      '#executes_submit_callback' => TRUE,
    );
  // Define a cancel button
    $form['cancel'] = array(
      '#type' => 'button',
      '#value' => t('Cancel'),
      '#executes_submit_callback' => TRUE,
    );

Notice the important step that I missed at first of '#executes_submit_callback'. Then in the submit function for the form, I had logic blocks like:

  // Determine which button was clicked
    $button = $form_state['clicked_button']['#value'] ;
  // Process the save button operation
    if (strcmp($button, t('Save')) == 0) {
      ... save operations ...
    }
  // Process the delete button opertaion
    if (strcmp($button, t('Delete')) == 0) {
      ... delete operations ...
    }
  // Cancel out of the form if the cancel button was entered
    if (strcmp($button, t('Cancel')) == 0) {
      ... cancel button operations ...
    }

Lars Toomre
Managing Director
Toomre Capital Markets LLC

http://lars.toomre.com/

Looks very straightforward - I'll give it a try!
Thanks*1.0E+06, Dave

I assume that code in the second code block (of Lars Toomre's post) would only work if the user clicked on one of the three buttons, as it requires $form_state to pass a 'clicked button' value to determine which button was clicked. However, I guess you could modify the submit function for the default button (the one that is activated when you press the 'enter' key) to include logic that checks which form fields passed data and then process accordingly. But this method may not be fool proof since other fields may contain data, so you would need to look carefully at what program logic you use. My gut feeling is that javascript/jQuery may still be your best bet. You could create a function which detects when the enter button has been used and then checks which field is in focus and trigger the appropriate action.

One could attach a javascript/jQuery script that reacts to an Enter click and it could determine which field then had the focus. One of the weaknesses though would be determining whether the field value really has been changed from when the form was first displayed. jQuery also is probably a weaker solution when one has a multi-field dependency. In such cases, it probably best to send the entire form back to the server for further processing.

Lars Toomre
Managing Director
Toomre Capital Markets LLC

http://lars.toomre.com/