Forms API and multiple submit buttons
Okay, I've looked in the docs and at some of the Drupal core modules, but I haven't been able to get this to work. I need to have a Drupal 4.7/5.0 form that has multiple submit buttons. I want each button to invoke the hook_validate() and hook_submit() functions, but I need a way to determine which button was the one actually pressed. Code like this is what I initially tried in my form: $form = array( .... stuff .... 'button1' => array( '#type' => 'submit', '#value' => t('Foo'), ), 'button2' => array( '#type' => 'submit', '#value' => t('Bar'), ), ); Now, both buttons work, but the form values array has both of their values. I tried using #default_value and #return_value instead of #value, but it seems not to change anything. Is there a better way to do this? Thanks! Scott (Syscrusher) -- ------------------------------------------------------------------------------- Syscrusher (Scott Courtney) Drupal page: http://drupal.org/user/9184 syscrusher at 4th dot com Home page: http://4th.com/
I need a way to determine which button was the one actually pressed.
In Drupal 5.0 you should be able to do something like if ($form_values['op'] == t('Button text')) {... (was only $_POST['op'] in 4.7 IIRC) in _validate and _submit to find which button was pressed. Rob Roy Barreca Founder and COO Electronic Insight Corporation http://www.electronicinsight.com rob@electronicinsight.com Syscrusher wrote:
Okay, I've looked in the docs and at some of the Drupal core modules, but I haven't been able to get this to work.
I need to have a Drupal 4.7/5.0 form that has multiple submit buttons. I want each button to invoke the hook_validate() and hook_submit() functions, but I need a way to determine which button was the one actually pressed.
Code like this is what I initially tried in my form:
$form = array( .... stuff .... 'button1' => array( '#type' => 'submit', '#value' => t('Foo'), ), 'button2' => array( '#type' => 'submit', '#value' => t('Bar'), ), );
Now, both buttons work, but the form values array has both of their values.
I tried using #default_value and #return_value instead of #value, but it seems not to change anything.
Is there a better way to do this?
Thanks!
Scott (Syscrusher)
You guys want to support http://drupal.org/node/81308 I think.
On Monday 20 November 2006 03:42, Rob Barreca wrote:
I need a way to determine which button was the one actually pressed.
In Drupal 5.0 you should be able to do something like if ($form_values['op'] == t('Button text')) {... (was only $_POST['op'] in 4.7 IIRC) in _validate and _submit to find which button was pressed.
Aha! I was missing the fact that you specifically have to test for "op" and not the fieldname of the button. I had seen the reference to $_POST['op'] in the code from node.module that I used as an example, but I just assumed they were naming their submit button "op". I didn't realize it was a special case. Thanks! Scott -- ------------------------------------------------------------------------------- Syscrusher (Scott Courtney) Drupal page: http://drupal.org/user/9184 syscrusher at 4th dot com Home page: http://4th.com/
participants (3)
-
Karoly Negyesi -
Rob Barreca -
Syscrusher