[Form API] Displaying results after form submission (and still displaying the form)

Created on 14 October 2010, almost 14 years ago
Updated 21 September 2024, 7 days ago

Hi,

I've been looking for days for the proper way to perform a query and display the results after a form has been submitted, while still displaying the form at the top of the results page. Something like standard search / filter forms do. I didn't want to use drupal_set_message() which is not appropriate since I don't want to return a status message, but a whole HTML section that should be printed below the form. The only post I could find was an unanswered Drupal 4.7 issue... :-/

I think I've found the proper way, could you please confirm the following code is OK so I can submit a new book page in the Form API 6.x section?

Let's consider a form whose $form_id is "mymodule_example_form". The function called on submit is "mymodule_example_form_submit". Here is the code :

function mymodule_example_form_submit(&$form, &$form_state) {

  // here, I would look into $form_state['values'] to get submitted values
  $my_interesting_value = $form_state['values']['XXX'];

  // here, I would then perform a custom query...
  $html = _mymodule_get_html_results($my_interesting_value);

  // ...and add the result to the form
  $form['#suffix'] = $html;

  // disable redirection (= return to the submitted form)
  $form_state['redirect'] = FALSE;

}

Notice that I've used as first argument, so I can alter the form object itself.

Thank you in advance for your review / remarks / suggestions.

📌 Task
Status

Active

Component

Correction/Clarification

Created by

🇫🇷France nclavaud

Live updates comments and jobs are added and updated live.
  • developer

    Used for Documentation issues that pertain to developer docs.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024