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 &form as first argument, so I can alter the form object itself.
Thank you in advance for your review / remarks / suggestions.
Comments
Comment #1
nclavaud commentedComment #2
wrburgess commentedI've got the same exact question if anyone can answer.
Comment #3
Sylvain_G commentedsubscribe
Comment #4
arianek commentedtags
Comment #5
jhodgdontagging
Comment #6
darrellduane commentedThanks so much for putting this in here, I've been looking all over for this, its working for me.
Comment #7
rfayI guess I would do this in the form body rather than in the submit.
The most standard way to do this (used by search, for example) is to turn the "interesting value" into a URL (GET params) and then have the page generation itself use *that* to generate the page (either generating it within the form builder, or building the page which includes a form and other data). The good thing about this is that it means the URL can be cached, that the search results can be achieved in other ways, etc.
So these models work:
$page = $form . $other_data_based_on_get_params
or
make the form builder function output your extra stuff based on $form_state
Your technique of tacking on #suffix in the submit is like the second of these. It doesn't have the advantages of the first one, but it works.
Comment #8
steven jones commentedRandy this is an incredibly common thing that you'd want to do, and there a so many ways to do it, would you accept a patch that adds a couple of examples to the Examples project that shows the different ways to implement such functionality and documents it? We could then pop a page in the handbook that points at the code.
(I'm not sure there is a page here that needs reviewing, so marking back to 'active' for now.)
Comment #9
nils_r commentedsubscribe
Comment #10
arianek commentedlooks like docs in the initial issue desc need review
Comment #11
avpadernoComment #12
quietone commentedThis is for Drupal 7, which is end of life.
Comment #13
avpaderno