Hi.

I have this form with a button on it:

$form['filter']['export'] = array(
	'#type' => 'button',
	'#value' => t('Export...'),
);

When I press the button I want it to perform an action by triggering a url, e.g. "admin/myform/export".

What would be the best way of doing this?

Thanks,
Luke

Comments

kannan@kiluvai.com’s picture

using _form_submit hook you can check the op values, based on the value you can do your action for example

function hook_form_submit($form, &$form_state){
if ($form_state['values']['op'] == 'Export') {
   \\do something

  } else if ($form_state['values']['op'] == 'submit') {
  \\ do something
}

}
pan69’s picture

Hi. Thanks for that. However, I don't think hook_form_submit exists for Drupal 6. Do you happen to know what I should use for Drupal 6 instead? I've been checking out the API but I can't seem to find it.

Thanks,
Luke

kannan@kiluvai.com’s picture

hi,

the example i shown is for drupal 6 only, if you want

go through this http://api.drupal.org/api/file/developer/topics/forms_api.html

in the above link see the Submitting Forms section where it explains form submitting

have a nice day

pan69’s picture

Hi. Sorry but I'm using Drupal 6. Then hook_form_submit is not being triggered when I implement it. I also can't find it in the API reference for 6...

I solved my problem by setting the '#action' property of the form when I build it:

$form['#action'] = url('path/to/execute');

Thanks,
Luke

glennnz’s picture

How do I do this when my button is in a node, rather than a form?

Thanks

Glenn

Glenn
THECA Group

glennnz’s picture

Bump

Can anyone help me do this on a node?

Thanks

Glenn

Glenn
THECA Group

andy2009’s picture

Hello,

thanks for your code but I have some problem while using above form action..

$form['#action'] = url('xxxx');
$form['#method'] = 'post';
$form['id'] = array ('#type'=>'hidden', '#value'=>$Id);

$form['submit'] = array ('#type'=>'submit',
'#title'=>t('Back'),
'#value'=>'Back',
'#size' => 40,
);

here I am trying to go back to previous page and I'm posting the Id value to that page...
but here when I clicked on Back submit it is not going back to previous page just refreshing in the same page.

Please help me with this..
Thanks in advance..

sahasra’s picture

i mentioned form action but validation not tigger it is directly goes to action url without validation

how to solve this problem

drupalguy854’s picture

Hello,

The link you've provided is a dead link. Do you know any solutions for this in Drupal 8?

EDIT: I've used WayBackMachine to view your shared link. Sorry for the bother.

https://web.archive.org/web/20120101195055/http://drupal.org/node/751826