How to show database query results in "Body" field?

4 replies [Last post]
mataylraj
Offline
Last seen: 10 years 12 weeks ago
Joined: 2011-06-26

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

mataylraj
Offline
Last seen: 10 years 12 weeks ago
Joined: 2011-06-26
Where does the "action" get mapped to?

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

mataylraj
Offline
Last seen: 10 years 12 weeks ago
Joined: 2011-06-26
Solution

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;
}

mataylraj
Offline
Last seen: 10 years 12 weeks ago
Joined: 2011-06-26
But what about conflict between two users?

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?

mataylraj
Offline
Last seen: 10 years 12 weeks ago
Joined: 2011-06-26
Okay, this solution is better

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;
}

Add to playlist

Add to , or

Add to new playlist:

Add to playlist
This is a member-only feature
But, it's easy to become a member

Add to cart:


Loading...
Contact us

Below are the supported keyboard shortcuts for our video player.

space
Start / stop video
shift
Go to next video
shift
Go to previous video
Increase or decrease player speed by 20%
Skip ahead or go back by 15 seconds
1...9
Jump to a percentage (2 is 20%, for example)
f
Toggle full screen
Videos Pricing More Search
BuildAModule Logo Icon BuildAModule Logo Icon
Become a member!
$29
1 month
$156
6 months
10% off
$276
12 months
20% off
All memberships come with the following:
24 / 7 access to the entire 1800+ video library
An innovative video player with a rolling transcript
Extensive Drupal and cross-platform training
A thorough and thoughtful training style
Progress tracking
Take notes on specific video sections
Intuitive multi-user management
How many users?
user
Go solo with a single user, or give multiple team members access through one account and get discounts by bundling multiple users into a single membership.