Redirecting Users After Submitting a Form in Drupal 5

Grab the feed

School is a twelve-year jail sentence where bad habits are the only curriculum truly learned.

John Taylor Gatto

Recent comments

If you create web sites based on Drupal you know that controlling forms, how they are themed, validated and processed, is a highly valued skill for any serious developer. This article will discuss a few ways to redirect your users to another page after they've submitted a form, overriding Drupal's defaults.

Drupal 5 provides developers with a Forms API, a clever way to craft HTML forms using logical and extremely flexible arrays.

Drupal Forms API also allows developers to theme every little detail of a form. For simpler cases using the #prefix and #suffix properties can be enough, but if you need extreme power, and sooner or later you will, then you can use forms-exclusive theme functions and drupal_render().

I'll explain the basics of Forms API in a future article, if you can't wait I suggest taking a look at the Forms API Quickstart Guide, for now let's focus in redirecting users after form submission in Drupal.

In the beginning was the form function

Every form in Drupal starts as a function that creates an array, usually named $form. Then the function's name is passed to drupal_get_form().

As every function in a module, a form creation function should start with the name of the module, for example:

boogeeks_notify_form()

First, this assumes my module is named boogeeks, the first word in my function's name. I'm separating each word with an underscore and I suggest you do the same.

Second, I've chosen the verb notify to have a clear idea of what the form will do. I used something like this in a recent project for hacking a form where users opt-in to receive email alerts.

Third, I added form, helpful when you have many functions in your module and need a quick way to tell apart the ones creating forms.

Notice that the only requirement for naming your form function is the first one, starting with your module name, the other two are my suggestions for cleaner and easier to understand code.

Theme, validate and submit

Now that we have boogeeks_notify_form() taking charge of form creation we need a few additional functions, all of them named based on the original.

  • theme_boogeeks_notify_form(): Will take care of the looks of your form, this is the theming function.
  • boogeeks_notify_form_validate(): Will check if user entered information that validates according to rules established here.
  • boogeeks_notify_form_submit(): Will process the form and redirect the user to other page.

For this article's purposes we are interested in the third function, the one ending in _submit.

Where to go now?

Every form submission function, boogeeks_notify_form_submit() in our example, needs to return a value, this value is the url where the user will go after successfully submitting a form.

So, if you want to thank your user after opting in to receive email alerts then you can finish boogeeks_notify_form_submit() with something like:

return 'thankyou-for-subscribing';

That's the url of some page with the usual thank-you-we-love-you-dear-visitor stuff.

All Drupal provided forms have pre-defined values in their submit functions. Two well known and often used ones are user_login() and user_register(), used to create new accounts, both are created in user.module.

How to override default form redirection in Drupal

There are two ways to override default form redirection, with a destination parameter in a url and using the #redirect property of Forms API. After some testing, I know this could be obvious to hardcore Drupal developers but it's still useful for new comers, I determined the redirection processing flow is as follows:

  • The url returned from the form submit function,
  • which is overriden by #redirect,
  • which is overriden by destination passed in the url.

You'll need to modify a form using hook_form_alter() to use #redirect.

If you want to work with destination you'll have to include it in the url used to call the page showing your form.

Now you can take your users whenever you want after submitting a form, no matter if it's a Drupal provided form or one that you've coded.

Bonus: got a headache with redirection after registration?

Yeah, me too, at least until finding a small bug in user.module, maybe that's your case.

Join the conversation

URLs are not paths

You mix up Drupal menu paths are URLs throughout the article. They are completely different things.

URLs and menu paths

Hi Steven, thank you for commenting. Now I'm a little confused with the terms.

If I correctly understand menu paths are the ones created from hook_menu and often related to a callback function, right? Let's say user/login and user/register in user_menu(), right?

What's what the return value in a _submit function, the destination parameter and $form['#redirect'], a url, right?

Please let me know what I'm missing in order to correct the article and be as accurate as possible.

Regards!

Alexis Bellido

workflow_ng

the new Workflow-ng can do the same job, easier for non-programmers.

Login Destination Module

http://drupal.org/project/login_destination
This module controls where users go after logging in. You can configure login destination on a settings page. It will be either static URL or a PHP snippet evaluating your own site's logic. You can also configure which pages redirection is applied.

I have inserted a small php

I have inserted a small php script on a node page, where the user must guess a number by writing it into the input box, now the page displays ok, but when the user clicks submit instead of returning the result that "click" will take you to the main page, can you help me with this please? thanks

Keep your comments relevant, written in good English and don't spam. Let's create useful and valuable discussions. Markdown is welcome.

Add your comment