Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
By DEThomas1953 on
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
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.
Drupal 6 Example
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:
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:
Lars Toomre
Managing Director
Toomre Capital Markets LLC
http://lars.toomre.com/
Drupal 6 Example
Looks very straightforward - I'll give it a try!
Thanks*1.0E+06, Dave
I assume that code in the
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
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/