DrupalDrupal 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


  1. fonant

    February 22, 2013 12:45 pm

    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.
  2. August 17, 2012 11:32 am

    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.
  3. Sandeep

    June 6, 2012 6:22 am

    Thanks a lot..:)
  4. October 14, 2011 4:50 pm

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

    May 7, 2011 8:41 am

    I google with multi submit and find this page, it helps me. Thanks.
  6. February 5, 2011 12:03 am

    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
    1. February 7, 2011 8:28 pm

      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 :) )

Post a comment

Your email address will not be published. Required fields are marked *