Home

Lullabot

Lullabot Ideas

We know stuff. We empower you to know stuff too.

On Site Drupal Training

We'll come to you! Graduate from your own on-site courses and become Drupalistas!

Drupal 5: Making forms that display their own results

Article by Jeff EatonNovember 22, 2006 - 2:54am

Drupal's Form API offers advanced features for validation, processing, and (in version 5) multi-part forms that span several pages. Sometimes, though, you need to do something simple: write a form that does nothing but display some formatted information based on the data that was submitted.

Here's a short snippet of code that demonstrates how Drupal 5's new #multistep flag (used for complex, multi-page forms) can also simplify the little problems.

<?php
function multistep_example_form($form_values = NULL) {
 
$form = array();
 
 
// Setting #multistep to true activates some special form
  // handling code for Drupal. First, the FormAPI makes sure
  // that the results of the form's submission are passed back
  // into the builder function when the page loads for the second
  // time. That gives it a chance to build a different version of
  // the form (with additional options, for example). It also
  // does magickal voodoo that keeps validation working properly
  // in those complex multi-page forms. For now, we won't
  // worry about that.
 
 
$form['#multistep'] = TRUE;
 
 
// Setting #redirect to false means that Drupal *won't* attempt
  // to reload a clean copy of the form (by redirecting to the
  // current page, and reloading) when it's submitted. In a multistep
  // form, we want to keep the data around so it can be displayed,
  // or passed on to a subsequent step.
 
 
$form['#redirect'] = FALSE;
 
 
// Remember: in a #multistep form, Drupal makes sure that the
  // $form_values array is passed into this builder function once
  // you submit the form. That gives us a chance to display different
  // form fields based on what was previously submitted.
 
 
if ($form_values === NULL) {
   
// We're entering the form for the first time. Display the form!
   
$form['text'] = array(
     
'#type' => 'textfield',
     
'#title' => t('Text field'),
     
'#required' => TRUE,
    );
   
   
$form['more_text'] = array(
     
'#type' => 'textarea',
     
'#title' => t('A bigger text field'),
    );
   
   
$form['submit'] = array(
     
'#type' => 'submit',
     
'#value' => t('Submit'),
    );
  }
  else {
   
// $form_values is populated, which means we're coming in a second
    // time. Let's display the results instead of the original form fields.
   
$form['results'] = array(
     
'#type' => 'item',
     
'#title' => t('The results of your form submission'),
     
'#value' => _multistep_example_format_values($form_values),
    );
  }
 
  return
$form;
}
 
function
_multistep_example_format_values($form_values = array()) {
 
$header = array(t('Key'), t('Value'));
 
$rows = array();
  foreach (
$form_values as $key => $value) {
   
$row = array();
   
$row[] = $key;
   
$row[] = check_plain($value);
   
$rows[] = $row;
  }
 
  return
theme('table', $header, $rows);
}
?>

That's all there is to it! Obviously, most modules will need to do some more complex formatting than that when they display their results. But the skeleton is there, and should serve you well.

Comments

esquillo (not verified) on November 27, 2006 - 4:14am

Thanks, Nice snippet :-)

Thanks,

Nice snippet :-)

Anonymous (not verified) on March 14, 2007 - 5:35pm

What about form_alter?

Your example assumes your are building a node module (e.g. hook_form).

How would (or would) this need to be modified to work on a module that was extending a node via hook_form_alter?

Lets say i have a nodeapi module taht wannts to add a second stage (part 2 of a multipart form) to the original node form ONCE ITS BEEN SUBMITTED. How would your example be modified to do that?

Could you provide a skeleton?

thanks much for the good articles

Anonymous (not verified) on April 23, 2007 - 9:14am

I am getting following

I am getting following error
Call to undefined function _multistep_example_format_values(). What may be the reason.

Anonymous (not verified) on September 4, 2009 - 3:18am

I presume you haven't

I presume you haven't included the function that was defined in this example.

Anonymous (not verified) on July 19, 2007 - 11:44am

Any ideas on how to integrating a pager table with results

I am trying to display the results across pages using the pager functions. how best can I do this - is it even possible?

thanks

Anonymous (not verified) on December 12, 2008 - 12:24pm

with pager...

I think the problem with combining this code and a pager_query is the test for $form_values:
if ($form_values === NULL)

When you click one of the links provided by the pager, no $form_values are passed and the form does not display any results. A quick workaround: change the above line to something like
if ($form_values !== NULL OR $_GET['page'])

That will allow results to be displayed either if the $form_values array is not empty (i.e. the form was just submitted), or if the $_GET array has a value in the 'page' element (i.e. you are trying to look at a paged result list).

Use at your own risk... this is probably not really the correct way to do this, but in some situations might be an OK workaround.

Anonymous (not verified) on December 16, 2008 - 9:43am

Could you possibly provide

Could you possibly provide an example?? I have trying for a week to implement pager in this with no luck....

Mahesh Vanneldas (not verified) on June 22, 2009 - 6:09am

Adding Pager to Search Form Results

Hey,

Do you have the example url with you, then please provide us the same.

We also want to implement the pager in our custom search results page.

Thanks in advance.
http://www.phppassion.com

Alexei A. Korolev (not verified) on July 24, 2007 - 5:29am

Great post

Great post, but any suggest how to replace standart /node/add form by own form?
Thanks.

Motor (not verified) on August 14, 2007 - 5:39am

Help me please

I am getting following error too. Help me please.

egorych (not verified) on August 15, 2007 - 6:38am

Thanks

I get errors too, but it's Ok, the idea is clear :). A usefull peace of code, really.

Anton Storozhuk (not verified) on August 17, 2007 - 8:22am

Great post!

Great post and great job! But the same thing...getting errors.

Leo (not verified) on September 10, 2007 - 7:04pm

Your suggestions are very

Your suggestions are very concrete and helpful. I’ll pass them on to writers who are maintaining their own blogs. Keep up the good work, its appreciated by the little guys..

CpILL (not verified) on September 18, 2007 - 11:43am

this just doesn't work

Copy pasted this in to a module I'm developing in Drupal 5.1 and I don't get to step 2. No values are passed to the form after the first submit?

Would dearly love to know how to get this goign as I've lost a day on it.

Andy (not verified) on September 27, 2007 - 8:56am

I'm seeing this in 5.2 too

I'm seeing similar behavior; sometimes you have to submit twice before the value actually makes it into the '$form_values' array.

Ken Colwell (not verified) on January 2, 2008 - 10:48am

Check for Caching

I had this same problem. Try using the cache exclude module to exclude the url of your form and this problem will go away.

Rémi (not verified) on January 15, 2009 - 10:12am

Same here

I'm having the exact same problem right now.

When submitting my form, my form function gets called twice and I need to submit my form twice before my new values are taken into account.

itchy (not verified) on October 15, 2007 - 3:10pm

I am using this for a complex search form but just one problem

This is a really useful article. I am trying to create my own complex search form that is displayed as a tab under the search page. This solution works really well except that I am displaying a sortable table. When I click on a sortable column on my table it redisplays the original form only. Can anyone offer suggestions on how to redisplay the sortable table?

Adam Young (not verified) on January 23, 2008 - 1:09pm

Can this work for the 'get'

Can this work for the 'get' form method? I tried it by simply adding:

$form['method'] = 'get';

...but now there are no values in $form_values.

Adam Young (not verified) on January 23, 2008 - 1:12pm

Using the 'get' method

Are we able to use the 'get' method with this technique? When I add this to the form:

$form['#method'] = 'get';

... the $form_values are empty.

Anonymous (not verified) on February 2, 2008 - 4:59pm

Can't wait to try it. I've

Can't wait to try it. I've been searching drupal.org for anything like this for countless hours. This is exactly what I wanted to do.

jranck88 (not verified) on June 4, 2008 - 4:53pm

Finally! I've been searching

Finally! I've been searching all over trying to find out how to do this. Should have know better and came to the lullabots first!

Cheers!
Joel

Vbest (not verified) on July 8, 2008 - 1:06am

thanks the post.

thanks the post.

akahn (not verified) on October 1, 2008 - 2:52pm

Awesome.

Cool, this is very helpful. I made a slight modification and didn't check if $form_values === NULL up top, and instead checked if $form_values != NULL, and if it is not null, I built the $form['results'] array, so that the form shows, with the user's value, along with the results. Perfect.

Single (not verified) on October 17, 2008 - 10:39pm

Very useful

Perfect. This is what I need.

Anonymous on January 22, 2009 - 12:33pm

HELP HELP HELP

Everything works great, apart from one thing: How can i add a pager to the displayed table? Please help, I really need this feature. Is pagination possible in this???

Grigoriy (not verified) on February 16, 2009 - 9:24am

Great

Thank you

Jimbo (not verified) on April 6, 2009 - 3:53pm

Upgraded to 5.16 data sent to e-mail not displaying

Hello,

I had an earlier version of Drupal and edited my article.module so as to display different fields for various areas on my site. All was working well until I upgraded to 5.16. Now the forms still display fine and e-mail the data but the formatting and field data are not in the submitted e-mail to me.

Before I got e-mailed:
Name: Joe Blow
Address: 90210
Phone:474-888-9999
Etc:

Now I get:

(persons name) has sent a message at (page name with form)

What could the problem be?

Please talk to me like I am six years old... I have racked my limited brain over this and it is fried.

Thanks!

Tawin (not verified) on June 8, 2009 - 7:25pm

How do you check for errors

Since the code only checks if values were submitted how do you check for errors during validation before running a query for your results? For example if it was a search form, and you don't want to run the search query unless the form passes validation. Actually in most cases you'd like to know if it passed validation before displaying any kind of result.

Jkushmaul (not verified) on August 1, 2009 - 4:17pm

The only table tutorial worth reading.

I don't think I have found one tutorial that just plainly, simply, explained how the hell to add table output in your form function.

It seems every article out there just has the code snippet, but not what function it is supposed to go in and that was the issue.

A nice combination is your tutorial and kpbowler.co.uk's tutorial:
[http://www.kpbowler.co.uk/drupal-tables-part-2]

But again, I could not have gotten kpbowlers to function without your to the point tutorial.

well done and thanks!

Jason

Hide (not verified) on September 22, 2009 - 2:39pm

Blocks

How use this for blocks?

About this 'bot

Jeff Eaton

Jeff Eaton is a long-time web developer. He's been designing, administering, and implementing web projects since he pieced together his first HTML file in 1996. He's built ecommerce sites for florists, helped implement enterprise web systems for multinational corporations, and juggled web architectures from legacy Perl to ASP.Net. (With some Windows desktop development thrown in for good measure...) Yes...

more

Recent

Drupal Voices 160: Moshe Weitzman on Page Rendering in Drupal 7

Podcast 9.02.2010

Drupal Voices 159: John Albin Wilkins on Drupal 7 Theming

Podcast 9.01.2010

Drupal Voices 158: Emma Jane Hogbin on PHP for Designers

Podcast 8.31.2010

Command Line Basics: More Editing with Vi/Vim

Video 8.31.2010

Lullabot's Back to School Sale

Blog 8.30.2010

Popular

Drupal Voices 160: Moshe Weitzman on Page Rendering in Drupal 7

Podcast 9.02.2010

Drupal Voices 159: John Albin Wilkins on Drupal 7 Theming

Podcast 9.01.2010

Drupal Voices 158: Emma Jane Hogbin on PHP for Designers

Podcast 8.31.2010

Installing Memcached on RedHat or CentOS

Article 8.20.2009

Photo galleries with Views Attach

Article 6.01.2009
 
  • Home
  • Services
  • Events
  • Ideas
  • Store

Connect the Bots:

Twitter Facebook YouTube blip.tv All Posts Newsletter
  • Ideas
  • Blog
  • Podcasts
  • Videos
  • About
  • Contact
  • Jobs
  • Services
    • Training
  • Events
    • Training Workshops
    • Other Events
    • Conferences
    • Calendar
  • Products
    • Videos
    • Books
    • Swag
  • Ideas
    • Blog
    • Podcast
    • Videos
  • About
    • Philosophy
    • Team
    • Presskit
  • Contact
    • General
    • Work Inquiries
    • Mailing List