欢迎各位兄弟 发布技术文章
这里的技术是共享的
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';
}
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
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
Just wanted to say thank you, this helped me as well with a form that has a dynamic number of buttons that redirect.
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.
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.
Multiple-Submit-Buttons/multiple_submit_buttons.module
<?php | |
// Implements hook_menu() | |
function multiple_submit_buttons_menu() { | |
$items['demo/multiple_submit_buttons/form'] = array( | |
'page callback' => 'drupal_get_form', | |
'page arguments' => array('multiple_submit_buttons_customform_form'), | |
'access callback' => TRUE, | |
'type' => MENU_CALLBACK, | |
); | |
return $items; | |
} | |
function multiple_submit_buttons_customform_form($form, $form_state) { | |
$form = array( | |
'name' => array( | |
'#type' => 'textfield', | |
'#title' => 'Enter your name', | |
'#maxlength' => '128', | |
), | |
'actions' => array( | |
'forward' => array( | |
'#type' => 'submit', | |
'#value' => 'Forwards', | |
), | |
'backward' => array( | |
'#type' => 'submit', | |
'#value' => 'Backwards', | |
), | |
), | |
); | |
return $form; | |
} | |
function multiple_submit_buttons_customform_form_validate($form, &$form_state) { | |
if ($form_state['values']['op'] == 'Forwards') { | |
drupal_set_message('This validation only runs when you click the forward button.'); | |
} elseif ($form_state['values']['op'] == 'Backwards') { | |
drupal_set_message('This validation only runs when you click the backwards button.'); | |
} else { | |
drupal_set_message('Unknown Action chosen'); | |
} | |
return TRUE; | |
} | |
function multiple_submit_buttons_customform_form_submit($form, &$form_state) { | |
if ($form_state['values']['op'] == 'Forwards') { | |
drupal_set_message('This is your string forwards: ' . $form_state['values']['name']); | |
} elseif ($form_state['values']['op'] == 'Backwards') { | |
drupal_set_message('You chose to print your input out backwards: ' . strrev($form_state['values']['name'])); | |
} else { | |
drupal_set_message('We aren\'t sure what you wanted to do, but your input is: ' . $form_state['values']['name']); | |
} | |
} |
来自 https://github.com/rvtraveller/Multiple-Submit-Buttons/blob/master/multiple_submit_buttons.module