1

A question exactly like this was asked last year, but this question was for Drupal 6, I'm wondering if there has been a change for Drupal 7.

Can I make the second or later button in a Drupal form the default button?

In my form, I have about four submit button form elements. Three of the four have a specified submit function and the other one uses the default hook_form_submit() function to handle it's submission.

So this is what three buttons almost look like:

$form['reset'] = array(
   '#type' => 'submit',
   '#value' => t('Reset Values'),
   '#submit' => array('_phonebook_reset_config'),
);

This is what the main button looks like:

$form['submit'] = array(
   '#type' => 'submit',
   '#value' => t('Update'),
);

This is the order in which the buttons are created in the form: (Button_1, Button_2, Main_Button, Button_4).

Right now if I press enter on the form, Button_1 gets executed. I want the main button to be the default button, so that when the user press enter, that main button is submitted not Button_1.

In the post mentioned above, one of the answers was to use weight. I tried using weight, but all that did was change the way the button was ordered on the screen. Yes this did allow the Main_Button, to be submitted, but then I needed that button in its original location.

Community
  • 1
  • 1
samwell
  • 2,757
  • 9
  • 33
  • 48

2 Answers2

2

This can't be done with just HTML. (So there's nothing Form API can do) As there is no standard and no html properties to handle a default submit in a multiple submit form. You should use CSS but if you don't wanna restyle you can use jQuery. Good luck!

 //Not tested
  $("#target").keypress(function(event) {
    if ( event.which == 13 ) {
       event.preventDefault();
       $('correctSubmit').submit();
     }
  });
Miguel Lomelí
  • 1,224
  • 1
  • 10
  • 17
  • JavaScript would have been the last resort. But if it there is no other way to do it other than through this, then this would have to do. – samwell May 15 '12 at 04:07
  • @chudapati09 It may require a little creativity with the styling but you can always use CSS. Anyways don't blame Form API as changing the default submit can't be done with just HTML. It's always the first one. – Miguel Lomelí May 15 '12 at 04:20
  • @chudapati09 I investigated and it seems that this is not a Form Api limitation. It just can't be done with just HTML. So don't feel bad for not being able to do it with PHP :) – Miguel Lomelí May 15 '12 at 04:26
  • Thanks for looking into it. This does make sense. – samwell May 16 '12 at 04:41
0

The solution identified at How is the default submit button on an HTML form determined? shows a pure HTML / CSS way to address this. I've expanded on the topic at http://pahsah.com/2015/08/10/html-forms-specifying-a-default-submit-button-pure-htmlcss-approach/.

Community
  • 1
  • 1
Shan Plourde
  • 8,528
  • 2
  • 29
  • 42