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';
}
February 22, 2013 12:45 pm
来自 https://naveensnayak.com/2010/03/09/drupal-multiple-submit-buttons/