Add cancel button to a form

2 replies [Last post]
jose
jose's picture
User offline. Last seen 35 weeks 13 hours ago. Offline
Joined: 03/30/2009

Some times is uselful to have another button like a "cancel" in a form so this is what you have to add to a form:

$form['cancel'] = array(
   '#type' => 'button',
   '#attributes' => array('onClick' => 'doSomething("value"); return false;'),
   '#value' => t('Cancel'),
);

jose
jose's picture
User offline. Last seen 35 weeks 13 hours ago. Offline
Joined: 03/30/2009
Great tip, thanks!

Great tip, thanks!

Anonymous
Anonymous's picture
javascript is one way to do it...

It's cleaner to do this without getting javascript involved:

$form['cancel'] = array(
  '#type' => 'button',
  '#value' => t('cancel'),
  '#executes_submit_callback' => TRUE,
  '#submit' = array('mymodule_form_cancel'),
);
[drupal6]

Then in mymodule_form_cancel() you can use drupal_goto() to send the user to a landing page, or do whatever you want.

Here's a more detailed writeup:

http://www.centurionwebdev.com/content/drupal-form-api-tip-adding-cancel-buttons