I have a simple Drupal Module setup which has a single textbox.

I can return what was typed in the textbox using drupal_set_message():

function firstmodule_form1_submit($form, &$form_state) {
    drupal_set_message(t("The user is $username"));

However echo $username; will not work for me, nothing is printed. How can I return results without using drupal_set_message()?


The normal way to handle this is to implement a multi-step workflow. See drupal_rebuild_form() for more information, but basically you set $form_state['rebuild'] = TRUE; in your submit handler, and in the original form function add a check to see if the form's been submitted. If it has, you have access to the submitted values and can manipulate the form as you see fit.

A very basic example:

function MYMODULE_some_form($form, &$form_state) {
  // If the form has been submitted, output something related to the submission
  if (!empty($form_state['values']['some_field'])) {
    $form['submission'] = array(
      '#markup' => function_to_get_markup_from_value($form_state['values']['some_field'])
  else {
    // Otherwise build the normal form
    $form['some_field'] = array(
      '#type' => 'textfield',
      // ...

  return $form;

function MYMODULE_some_form_submit($form, &$form_state) {
  $form_state['rebuild'] = TRUE;
|improve this answer|||||

A form submission handler can only print a message using drupal_set_message(), or writing a message in the log with watchdog().

The alternative is saving the necessary data in the session, and redirect the user (using $form_state['redirect'] = 'page path';) to a page where the content of the session is shown.

function firstmodule_menu() {
  $items['the path for your page'] = array(
    'page callback' => 'firstmodule_show_submitted_data';
    // ...

  return $items;

function firstmodule_form1_submit($form, &$form_state) {
  $_SESSION['firstmodule_username'] = $username;
  // ...

  $form_state['redirect'] = 'the path for your page';

function firstmodule_show_submitted_data() {
  // Populate $result with the string to show.

  return $result;

As side note, the first argument of t() must be a literal string, not a string obtained concatenating two strings, as in your case. You should use placeholders, as in the following code.

drupal_set_message(t("The user is %username", array('%username' => $username)));

In Drupal 7, instead of returning a string, you can return a render array.

|improve this answer|||||

All submitted data stored in $form_state, if u want to see what happens install devel module and after you always can do like this:

Inside your firstmodule_form1_submit function to print all the array $form_state type - dpm($form_state) - and you will see output, where u can find your textbox username or any other, will be something similar to this $form_state['values']['username']

And then you can just print it drupal_set_message(t($form_state['values']['username']);

|improve this answer|||||
  • Thanks, I have that part however drupal_set_message returns it as a "Help Message" type thing. I would like to return lots of content on the page itself. – h00j Dec 29 '12 at 12:43

What you need to do is to have a menu callback, basically that would be a page you set up and display the info on.

More info is available here: http://api.drupal.org/api/drupal/includes!menu.inc/group/menu/7 and you need to read more about hook_menu()

Hope that helps, tell me if you need an example.

|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.