I have 2 submit buttons and want to perform different actions for each submit button . Here I want to set the form fields which can be done only in form_alter(). Can anyone suggest how to check for multiple submit buttons in the form_alter() function?
I have used
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';
}
but this does not work
The best thing to do if you have two submit buttons on a form and want them to do different things, is to create a different submit function for each button, and connect them up. One of the nice things about the FormAPI is that it automatically links the form with the submit handler for you, but if you have two submit buttons you want to go somewhere new.
So your form code is likely to contain:
But I don’t know what you mean by form_alter() – there’s no reason to use a form alter of any sort.
Edit: As came up in the comments – if you need different validation functions for the two buttons, you can also include
'#validate' => array('my module_form_validate_one')and'#validate' => array('my module_form_validate_two')in the respective button arrays. But it’s not required, and if the standard form validation function works fine then go with that.