hi there...
i wanted to know a way for handling multiple submit buttons and their click events..
here i have a form where i have two buttons ... search and save...
When the user clicks on search, the page is redirected to myform/search
and else, myform/save ...
I have been trying to use the condition:

<?php
if($form_state['values']['search'] == 'Search'){
$form_state['redirect'] = 'myform/search/'.$form_state['values']['compname'];
}
$form_state['redirect'] = 'myform/save/'.$form_state['values']['compname'].'/'.$form_state['values']['street'].'/'.$form_state['values']['locality'].'/'.$form_state['values']['city'].'/'.$form_state['values']['pin'].'/'.$form_state['values']['state'].'/'.$form_state['values']['country'].'/'.$form_state['values']['description'].'/'.$form_state['values']['headedby'].'/'.$form_state['values']['contactperson'].'/'.$form_state['values']['phone'].'/'.$form_state['values']['email'].'/'.$form_state['values']['website'].'/'.$form_state['values']['branch'].'/'.$form_state['values']['category'];

}
?>

This jus doesnt go in the if loop and runs out and redirects to the myform/save page ...

Can you help?!?!

Thanks,
Radhika

Comments

jeremy_a’s picture

Hi radhika,

Try:

if($form_state['values']['clicked_button']['#value'] == 'Search'){
...

Your two buttons should have different #value names - I think you're ok because you've got 'Search' and 'Save'. But in the case where they have the same name -- eg two 'save' buttons, you need to give them seperate names with the #name property.

radhika.w’s picture

hi..
thanks for the reply....
But it does not help...
It gives me exactly the same results as before ...

Radhika

j.somers’s picture

You have to use an 'else' clause.

$a = 1;
if ($a == 1) {
  $b = 2;
}
$b = 3;
print $b; // This will always print out the number '3'.

$a = 1;
if ($a == 1) {
  $b = 2;
}
else {
  $b = 3;
}
print $b; // This will print out the number '2'.

Why are you actually doing such a redirect when saving? It's a lot easier (and the URL is a lot cleaner) when you perform the actual save in the _submit() function and you redirect to a nice OK page after the save has been completed.

radhika.w’s picture

hi thanks for the idea .. :)
i had actually thought about it ...
but got flown into the multiple submit buttons problem !!
i will try that and let u know
thanks again,
radhika

radhika.w’s picture

thanks j.somers .... for that idea...
It really helped me to sor all my functions..
Now i can edit delete and update data through the same form...
Thanks a million!!!
Enjoy,
Radhika

hutch120’s picture

There is a bit more to adding multiple submit buttons in Drupal than at first glance. I didn't find an article that covered all the elements I needed, so here is a condensed version of how I did it.

1) You need to add the buttons and call a different function depending on which button is clicked, this is fairly easy and there are a number of tutorials about this online. Basically this is how you do that.

In the form_alter function of your Drupal module you can add lines like this to add new buttons.

You have to add 'node_form_submit' to the default submit button if you also want to add a second function call on the same button.

$form['buttons']['submit']['#submit'][] = 'node_form_submit';
$form['buttons']['submit']['#submit'][] = 'mymodule_formname_button1_form_submit';
$form['buttons']['button2']['#type'] = 'submit';
$form['buttons']['button2']['#value'] = 'Save Only';
$form['buttons']['button2']['#weight'] = 7;
// This will do all the stuff to submit the node correctly.
$form['buttons']['button2']['#submit'][] = 'node_form_submit'; 
// This will do extra stuff, must be second.
$form['buttons']['button2']['#submit'][] = 'mymodule_formname_button1_form_submit'; 

2) For the default button I did a redirect to a URL with parameters and using the node ID. Note the use of array was necessary to avoid URL mashing (probably not the technical term).

function mymodule_formname_button1_form_submit($formID, &$form_state) {
 $nid = $form_state['nid'];
 $form_state['redirect'] = array('another-path', array('param1'=>$nid, 'param2'=>'new'));
}

The array is needed to avoid the URL being mashed.

3) For Button 2 I did a redirect to a URL without any parameters using the node ID.

function mymodule_formname_button2_form_submit($formID, &$form_state) {
 $nid = $form_state['nid'];
 $form_state['redirect'] = 'node/' . $nid  . '/edit';
}

References:
http://drupal.org/node/247585