How to show database query results in "Body" field?
Folks,
I have a database table of book titles, authors, etc. I created a module that creates a "librarysearch_form" that allows the user to search the database table based on filters for author, title and other criteria. I used the Form API and the Database API. But how do I get from the search form that I created to the results page? I want to somehow put my results in the body region of a page. Do I put that code in the librarysearch_form_submit function, and if so, once I get all the HTML built, how do I inject it into the body region of a page? Can all this code go in the same module as the code I used to build the search form?
Thanks,
Matt
Okay, I figured it out after studying the Node API videos.
In the librarysearch_form function I added the following:
$form['#action'] = url('node/2');
And then the submit function looks like this (puts the search results in node 2 body region):
function librarysearch_form_submit($form, $form_state){
if ($node = node_load(2)) {
$node->title = "Search Results Page";
$node->body['und'][0]['value'] = 'Put search results here';
node_save($node);
$build['node_display'] = node_view($node);
}
return $build;
}
But my proposed solution may have a problem if two users are trying to do a library search at the same time. I wonder if there's a way to display the search results outside of a node, like with a hook_menu call or something. I just wouldn't know how to pass it the search filter criteria, though. We can't use $_REQUEST if we're using the form API, right?
Okay, I finally figured out the right way to do what I was trying to do. The following very simple module code is a two-step form that asks you your name in step 1 and then says "Hello $name" in the body field on step 2.
I found this link http://drupal.org/node/1121110 very helpful, specifically the form example, tutorial #8, but there are a lot of other good example modules in there as well.
The submit function (which I copied from the example referenced above) is what makes it special... it makes the form state accessible the second time the form is accessed so you can display a different form.
<?php
/**
* Implements hook_menu().
*/
function simpleexample_menu() {
$items['simple-example-page'] = array(
'title' => 'Simple Example Form',
'page callback' => 'drupal_get_form',
'page arguments' => array('simpleexample_form'),
'access callback' => TRUE,
'type' => MENU_LOCAL_TASK,
);
return $items;
}
// Builds either page 1 or page 2 of the form
function simpleexample_form($form, &$form_state) {
// Display page 2 if $form_state['page_num'] == 2
if (!empty($form_state['page_num']) && $form_state['page_num'] == 2) {
return simpleexample_form_page_two($form, $form_state);
}
// Otherwise we build page 1.
$form['your_name'] = array(
'#title' => t('Your first name?'),
'#type'=>'textfield',
);
$form['submit'] = array(
'#type'=>'submit',
'#value'=>t('Submit'),
'#submit' => array('simpleexample_submit'),
);
return $form;
}
function simpleexample_form_page_two($form, &$form_state) {
$name = $form_state['values']['your_name'];
$form['markup'] = array(
'#type'=>'markup',
'#markup'=> "<p>Hello $name</p>",
);
return $form;
}
function simpleexample_submit($form, &$form_state) {
// Values are saved for each page.
// to carry forward to subsequent pages in the form.
// and we tell FAPI to rebuild the form.
$form_state['page_values'][1] = $form_state['values'];
if (!empty($form_state['page_values'][2])) {
$form_state['values'] = $form_state['page_values'][2];
}
// When form rebuilds, it will look at this to figure which page to build.
$form_state['page_num'] = 2;
$form_state['rebuild'] = TRUE;
}
I guess the main thing that confuses me is the following. If I am using straight HTML, I start my form like this:
<
form action="some_url">
In the drupal forms API, what happened to that "action" portion? Where do I put that? I don't see that information in any of the videos.
Thanks,
Matt