views:

672

answers:

2

How would I print the results of a form submission on the same page as the form itself?

Relevant hook_menu:

    $items['admin/content/ncbi_subsites/paths'] = array(
        'title' => 'Paths',
        'description' => 'Paths for a particular subsite',
        'page callback' => 'ncbi_subsites_show_path_page',
        'access arguments' => array( 'administer site configuration' ),
        'type' => MENU_LOCAL_TASK,
    );

page callback:

function ncbi_subsites_show_path_page() {
  $f = drupal_get_form('_ncbi_subsites_show_paths_form');
  return $f;
}

Form building function:

   function _ncbi_subsites_show_paths_form() {
      // bunch of code here

      $form['subsite'] = array(
        '#title' => t('Subsites'),
        '#type' => 'select',
        '#description' => 'Choose a subsite to get its paths',
        '#default_value' => 'Choose a subsite',
        '#options'=> $tmp,
      );

      $form['showthem'] = array(
        '#type' => 'submit',
        '#value' => 'Show paths',
        '#submit' => array( 'ncbi_subsites_show_paths_submit'),    
      );

      return $form;
    }

Submit function (skipped validate function for brevity)

function ncbi_subsites_show_paths_submit( &$form, &$form_state ) {
  //dpm ( $form_state );
  $subsite_name = $form_state['values']['subsite'];
  $subsite = new Subsite( $subsite_name ); //y own class that I use internally in this module
  $paths = $subsite->normalized_paths;

  // build list
  $list = theme_item_list( $paths );
}

If I print that $list variable, it is exactly what I want, but I am not sure how to get it into the page with the original form page built from 'ncbi_subsites_show_path_page'. Any help is much appreciated!

+1  A: 

The key information in the link Nikit posted is $form_state['rebuild']. Here's some info from Drupal 7 documentation that I believe applies the same for Drupal 6...

$form_state['rebuild']: Normally, after the entire form processing is completed and submit handlers ran, a form is considered to be done and drupal_redirect_form() will redirect the user to a new page using a GET request (so a browser refresh does not re-submit the form). However, if 'rebuild' has been set to TRUE, then a new copy of the form is immediately built and sent to the browser; instead of a redirect. This is used for multi-step forms, such as wizards and confirmation forms. Also, if a form validation handler has set 'rebuild' to TRUE and a validation error occurred, then the form is rebuilt prior to being returned, enabling form elements to be altered, as appropriate to the particular validation error.

Got it working, thanks to this tip. However,the only way I could "pass" data from the submit function to the function that renders the form is by setting global variables in the submit function, then rebuilding the form, and checking if those varaibles are set. Is there another way to do this? I don't like global variables.
Aaron
Also in the example from that link, you can see $form_state['storage'] being used. I believe ['storage'] is a dedicated place where you can safely store data between validations, submits etc.
The problem is that I don't have access to $form_state from the page handler function. I'd like to store something in the storage array of $form_state, but I need that value in the page handler, not the form building function...
Aaron
Drupal will pass in the $form_state variable to your form building function if you put it in the function definition -- it will never be passed to the page callback, as *that* is outside of form API's scope. Inside your form builder function you can switch based on the contents of form state and add extra elements of type 'markup' containing the results you want to display.
Eaton