A Drupal form submit button and submit handler function example

Drupal form submit button and handler FAQ: How do I create a Drupal form with a Drupal submit button? Also, how do I create a Drupal form submission handler function?

Assuming that you're comfortable with creating a Drupal form, you can add a submit button to your Drupal form very easily, like this:

# a drupal form submit button
$form['submit'] = array(
  '#value' => t('Save'),
  '#type' => 'submit',
);

When you create a Drupal form submit button like this, Drupal renders a button in your form with the label "Save" on the button. You can change that value to anything you want.

The Drupal submit button handler function

When the user clicks your submit button, Drupal will look for a submit button handler, which is a PHP function that you need to define. (How you handle the form submission process is up to you, after all.)

If you don't specify the name of a submission handler function in your Drupal form definition, Drupal will look for a submit handler function that follows the default Drupal naming convention. Here's how this naming convention works.

When I first define my Drupal form I include a 'page callback' definition like this:

'page callback' => 'firstmodule_form',

This tells the Drupal form system that I will later have a function defined like this:

# define my drupal form id
function firstmodule_form() {
  return drupal_get_form('mymodule_form1');
}

The string 'firstmodule_form' connects the 'page callback' to this function name.

This function defines the id of my form to have the name 'mymodule_form1', so by default Drupal will now look for a form submission handler of the name 'mymodule_form1_submit'. Drupal simply derives this name by adding '_submit' to your form id.

Now that Drupal has a name for my form submission handler, I can define my submission handler like this:

# our drupal form submit handler for a module named 'foo'
function mymodule_form1_submit($form, &$form_state) {
  # add your submit handler logic here, or in a very simple case,
  # just render a message like this:
  drupal_set_message(t('Thanks, your form has been submitted.'));
}

Of course you'll want to do more work here related to your form variables, and you'll probably want to include a Drupal form redirect statement, but hopefully this is enough to get you started in the right direction.

Specify your own form submission handler name

If you don't like the default Drupal naming convention, you can instead specify a name of a form submit button handler like this:

# a drupal form submit button
$form['submit'] = array(
  '#value' => t('Save'),
  '#type' => 'submit',
  '#submit' => array('foo_handler'),
);

In this example, when the form's submit button is pressed, Drupal will call the function named 'foo_handler' to handle the form processing, so you now need to define a Drupal form submission handler like this:

function foo_submit($form, &$form_state) {
  # add your logic here
}

Drupal form submit button examples - Summary

I hope this Drupal form submit button example has been helpful. If you have any questions just leave a note in the Comments section below and I'll be glad to provide more information.