Community Documentation

Adding buttons to a form

Last updated July 28, 2010. Created by Steven Jones on May 25, 2008.
Edited by boombatower, acrollet. Log in to edit this page.

When your users have entered data, they're going to want to do something with it: save it, upload it or be able to add more of it. Usually these actions are implemented by buttons. Fortunately buttons are as easy to add to Drupal forms as fields (because actually they're a type of field!)

Add the following to your form builder function:

<?php
$form
['my_button'] = array(
 
'#type' => 'submit',
 
'#value' => t('Perform Action'),
);
?>

And Drupal will render a lovely button with the (translated) text 'Perform Action', and when clicked the form's submit (and validation) handlers will be called.

You can add more than one submit button to a single form array, and cause different submit handlers to be executed. Lets say that I want my secondary_submit_function() to be called when a user clicks on the 'Alternative Action' button. I would add:

<?php
$form
['alt_button'] = array(
 
'#type' => 'submit',
 
'#value' => t('Alternative Action'),
 
'#submit' => array('secondary_submit_function'),
);
?>

The form's validation handlers will still be called, but only the secondary_submit_function() submit handler will be called. For more information about submit handlers see the SECTION NAME section.

About this page

Drupal version
Drupal 6.x
Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here