1

I am creating an example report of user data based on selection criteria selected in a form. I can create the form variables, and I can redirect the user to the report output page, but how do I include the selection criteria variables in the redirect? Here is my module code:

<?php
/**
 * @file
 * Example form module that creates a users report.
 */

/**
 * Implements hook_menu().
 */
function example_form_menu() {
  $items = array();
  $items['example-form'] = array(
    'title' => 'Example Form',
    'description' => 'Provides search criteria for the Users report',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('example_form_create_form'),
    'access arguments' => array('access content'),
    'type' => MENU_NORMAL_ITEM,
  );

  $items['example-form-output'] = array(
    'title' => 'Report output',
    'description' => 'Output page for Users report',
    'page callback' => 'example_form_output_page',
    'access arguments' => array('access content'),
  );
  return $items;
}

/**
 * Creates a form.
 */
function example_form_create_form($form, &$form_submit) {
  $form['title'] = array(
    '#title' => t('User'),
    '#type' => 'textfield',
    '#description' => 'Search for a User.',
  );

  $form['user-date-created-comparison'] = array(
    '#title' => t('Creation Date'),
    '#type' => 'select',
    '#options' => drupal_map_assoc(
      array(
        t('is greater than:'),
        t('is less than:'),
        t('is equal to:'),
      )
    ),
    '#empty_option' => '',
  );

  $form['user-created-date'] = array(
    '#type' => 'date_popup',
    '#date_format' => 'm/d/Y',
    '#date_label_position' => 'within',
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Submit',
  );

  return $form;
}

/**
 * Implements hook_form_alter().
 */
function example_form_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == "example_form_create_form") {
    $form['submit']['#submit'][] = 'example_form_redirect_handler';
  }
}

/**
 * Attaches the redirect to the submitted form.
 */
function example_form_redirect_handler($form, &$form_state) {

  $form_state['redirect'] = 'example-form-output';
}

/**
 * Creates the report output page.
 */
function example_form_output_page() {

  // Here is where I am stuck. How do I access the form variables?

  // Call db_select()

  $html = "<h1>Report Output:</h1>";

  // Display users, roles, creation date, etc. based on the search
  // criteria supplied in the form.

  return $html;
}
4

I think you pass some arguments when you redirect the page to another page. You can pass arguments like this way..

$form_state['redirect'] = array(
  'node/123',
  array(
    'query' => array(
      'foo' => 'bar',
    ),
    'fragment' => 'baz',
  ),
);

Or You also can use drupal_goto() like this

drupal_goto('your_url', array('query'=>array(
'variable_name1'=>'variable_value1',
'variable_name2'=>'variable_value2',
'variable_name3'=>'variable_value3',
...... ...
)));

You can check below links for more information. https://api.drupal.org/api/drupal/includes!form.inc/function/drupal_redirect_form/7 https://www.drupal.org/node/291680 https://api.drupal.org/api/drupal/includes%21common.inc/function/drupal_goto/7

Note : Ignore to use this options. And last options is $_SESSION variable. you can store your data in session and after getting data from session in your destination page you unset that particular session data.

  • I agree with this suggestions except using drupal_goto directly. If drupal_goto is used in a submit handler submit workflow is broken. It may work, but when another submit hanlder is needed that goes after the submit with drupal_goto it will fail as it can be executed. – sanzante Mar 5 '15 at 10:50

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.