I have a question related to drupal 7 form api and module development.

I have a very simple module with a function like this. It gets called by hook_menu when a certain path xyz is visited.

function myview_mainpage() {

  $content .= drupal_render(drupal_get_form('myview_form'));
  $content .= "Some additional stuff here";
  return $content;

My form looks like this (it's from the examples module):

function myview_form($form, &$form_state) {
  $form['description'] = array(
    '#type' => 'item',
    '#title' => t('A simple form with a submit button'),

  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Name'),

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

And my submit handler looks like this:

function myview_form_submit($form, &$form_state){

 // I do some sql stuff and generate html content based on the value that was submitted in the form
  $variable = "<div><h1>Some stuff</h1>...'; 


And now I need to somehow pass the value of $variable to the function myview_mainpage() so it can print it out. So in a nutshell: User submits a from, some processing is done based on the value submitted and then the results are displayed under the form when it is displayed.

Maybe I could use $form_state['redirect'] to redirect the user to the page xyz but I don't want to send the value of $variable in the address bar of a browser.

Any ideas? This feels like it should be done quite easily but I can't find a way to do it.

  • I would include the menu function too, so new users would know
    – iStryker
    Jul 12, 2011 at 21:31

5 Answers 5


You can store that information in the user session. There is always a redirect after a form submission so you need to persist it somewhere because PHP is a stateless.

Simply add your data to $_SESSION['yourmodule'] and then in the mainpage() function check if something is there and display it.

If that information is not temporary and must be there on the next visit too, persist it in the database in a custom table.


Drupal way of doing this would be to do it in a theme


Used by: All elements and forms.

Description: Theme function to call for element. It must render this element and all of its child elements.

Values: The name of a theme function, without the initial 'theme_'.

add $form['#theme'] = 'myview_form' to your form function

then use

function theme_myview_form($variables) {


myview_mainpage() will be simpler

function myview_mainpage() {
  return drupal_render(drupal_get_form('myview_form'));

I don't think you need form_submit() for what you are trying to do.

  • ...all the variables to get the job done should be in $variables
    – iStryker
    Jul 12, 2011 at 21:49
  • #theme is for rendering the form. He is asking about displaying the information submitted by the user through the form after it has been submitted, outside of the form.
    – Berdir
    Jul 12, 2011 at 22:15
  • ah, I think I get the question better now. He want to click submit, be redirected to the same page. Form is displayed again, with results from the preview submit underneath
    – iStryker
    Jul 12, 2011 at 22:31

First off, drupal_render(drupal_get_form('myview_form')) would cause an error, since drupal_render() is expecting a value passed by reference, but drupal_get_for() is not returning a reference.

Rather than having a page callback that render a form, I would use the form builder for the page at the xyz path. The submission form handler, and the form builder for which the submission handler is called can share values through the $form_state variable, which is passed to both the functions, and which is passed as reference to the form submission handler.
The form submission handler should have the following line.

$form_state['rebuild'] = TRUE;

What the form builder should do is:

  • Verifying the value set by the form submission handler in $form_state is set

  • Using that value for rendering it before, or after the form

  • Adding the form elements as a form build function would normally do

In this way, there would not be any need to pass data into the session, save the data in a database table, or use variable_get()/variable_set().


variable_set('id_name','value'); and after that variable_get('id_name');. After you don't need anymore the value, remove it.

  • 4
    User submitted data must not be saved in shared space! Unless ... well unless it's shared/public data.
    – rik
    Jul 30, 2012 at 18:01

the right way (drupal way) to do this is using hook_preprocess. For example:

function MODULE_forms_preprocess_results_template(&$vars) {

then generate whatever you need and inject it in the form

$vars['your_variable'] = somefunction();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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