Search code examples
drupaldrupal-fapidrupal-5

How do I add two submit buttons to a Drupal 5 form?


I have two submit buttons on my form. One is the normal submit button that calls `my_form_submit` like-ah-so:

$form['fieldset']['submit'] = array(
  '#type' => 'submit',
  '#value' => 'Show Applications',
);

I want to add another button that will submit the form, but call a different submit handler. Is there any way to do this? I have looked at adding the #submit property to an element like-ah-so:

$form['fieldset']['evil_plan'] = array(
  '#type' => 'submit',
  '#value' => 'Big Red Button',
  '#submit' => array('steal_formula_for_icantbeliveitsnotbutter'),
);

hoping to call the `steal_formula_for_icantbeliveitsnotbutter` function but no worky. When I click the Big Red Button the normal `my_form_submit` function is called, which sets back my plan to make millions off of a butter substitute. Who can shed some light on this dark mess?


Solution

  • for Drupal 5, this is the only solution:

    function my_form_submit($form_id, $form_values) {
      if ($form_values['op'] == 'Show Applications') {
        // handle 'Show Applications' submission
      }
      elseif ($form_values['op'] == 'Big Red Button') {
        // handle 'Big Red Button' submission
      }
    }
    

    same for my_form_validate.

    in Drupal 6, this can easier be done with the newly introduced custom #submit and #validate handlers for form buttons.