Drupal - How to redirect a user when altering an existing form

Drupal form FAQ: How do I redirect a user when they submit a form, and I'm writing a hook_form_alter function to alter that form?

In Drupal 6 you set the "$form['#redirect']" property in your hook_form_alter function when you alter the form, like this:

$form['#redirect'] = 'thank-you';

That syntax tells Drupal to redirect the user to the "/thank-you" URI on your website.

Here's the source code for a full hook_form_alter function I just pulled from a working Drupal module:

 * Implementation of hook_form_alter().
function user_validator_form_charity_node_form_alter(&$form, &$form_state) {

  $form['bookverify'] = array(
      '#type' => 'fieldset',
      '#title' => t('Confirmation')
  $form['bookverify']['words'] = array(
      '#type' => 'textfield',
      '#title' => t('Enter the secret code'),
      '#description' => t('Lorum ipsum yada yada'),
      '#required' => TRUE,
  # validate function not shown here
  $form['#validate'][] = 'user_validator_charity_validate';

  # set the form submission redirection property
  $form['#redirect'] = 'thank-you';

Drupal hook_form_alter function naming syntax

While I'm in the neighborhood, it's important to know that your form "alter" function must be named according to the Drupal naming convention. Specifically the alter form function must be named according to this convention, with each field separated by the underscore character:

modulename + 'form' + formID + 'alter'

So a modulename of "foobar" with a form id of "baz" will look like this:


and its full signature will look like this:

function foobar_form_baz_alter(&$form, &$form_state)

In summary, I hope this information on how to redirect the user when they submit a form that you have altered has been helpful. I don't know if this same approach works in Drupal 7, but if you find that it does, I'd appreciate it if you'd leave a comment below.