Home > Drupal > Drupal 6: Multiple Submit buttons

Drupal 6: Multiple Submit buttons

This is a small piece of code that can be used when you have multiple submit buttons in the same form in Drupal. By using the “clicked_button” in submit function, we can redirect to a certain page based on the button clicked.

/*
 * Drupal form function
 */
function myform_form() {
    //button 1
    $form['heading'] = array (
        '#type' => 'markup',
        '#value' => t('Welcome to My FORM!!'),
    );
   $form['button_one'] = array (
        '#type' => 'fieldset',
        '#title' => 'BUTTON 1'
    );
    $form['button_one']['desc'] = array (
        '#type' => 'markup',
        '#value' => t('This is button 1 text'),
    );
     $form['button_one']['submit_one'] = array (
        '#value' => t('Button 1'),
        '#type' => 'submit',
    );

    //button 2
    $form['button_two'] = array (
        '#type' => 'fieldset',
        '#title' => 'BUTTON 2'
    );
    $form['button_two']['desc'] = array (
        '#type' => 'markup',
        '#value' => t('This is button 2 text'),
    );
     $form['button_two']['submit_two'] = array (
        '#value' => t('Button 2'),
        '#type' => 'submit',
    );

    return $form;
}

This is the submit function for the form

/*
 * Submit function
 * Use the clicked_button functionality
 */
function myform_form_submit($formID, &$form_state) {
    if($form_state['clicked_button']['#value'] == $form_state['values']['submit_one'])	  //if button 1 is clicked
        $form_state['redirect'] = 'mypath/page_one';	//redirect to whatever page you want
    else if($form_state['clicked_button']['#value'] == $form_state['values']['submit_two'])  //if button 2 is clicked
        $form_state['redirect'] = 'mypath/page_two';
}
Categories: Drupal Tags:
  1. February 5th, 2011 at 00:03 | #1

    Hi Naveen,

    your article helped me with my problem, my forms work now due to your hint. But: In Drupal 6.20 it is sufficient to check the content of $form_state[‘clicked_button’][‘#value’] in order to obtain the required button. A field set is not required. “clicked_button” is the key, thank you very much.

    Kind regards, Erik

    • February 7th, 2011 at 20:28 | #2

      Eric, i am glad that it helped you
      The field-set is there just for visual differentiation of the two buttons ( it has nothing to do with the submit function 🙂 )

  2. Chong
    May 7th, 2011 at 08:41 | #3

    I google with multi submit and find this page, it helps me. Thanks.

  3. October 14th, 2011 at 16:50 | #4

    Just wanted to say thank you, this helped me as well with a form that has a dynamic number of buttons that redirect.

  4. Sandeep
    June 6th, 2012 at 06:22 | #5

    Thanks a lot..:)

  5. August 17th, 2012 at 11:32 | #6

    Note that if you’re wanting to have similar-looking sets of buttons, searching for value doesn’t always work — for instance, I had “move up” and “move down” buttons in each fieldset, and having “move up 1” and “move down 3” is confusing and doesn’t make sense (Why didn’t I use tabledrag or something instead, you ask? I was build a Panels content type admin form, which really don’t play nicely with Javascript or anything remotely ajax-y, forcing me to use a purely FAPI implementation.).

    In such a case, you might want to set the ‘#name’ attribute for each button and then test against that instead.

  6. fonant
    February 22nd, 2013 at 12:45 | #7

    If all your buttons have the same name (e.g. “op”) then ‘clicked_button’ returns the last of them all the time. This can be fixed by setting the #names of the buttons explicitly, such as “op1”, “op2”, “op3”, and so on. You have to start the #name with an alphabetical character, it can’t be just a number.

  1. No trackbacks yet.