A simple question on a basic operation: show results of a query using form.

I have a table with values. I have created a form, with which I can query the table contents.

Example -
Employee Table:
Name | Department | Designation
Scott | Accounts | Accountant
Jake | Accounts | Officer

My form gives a text box, where the user can enter the employee name (Scott, or Jake, or something else). I want the result to be displayed in a neat table. Redirecting to another page is fine with me.

I have built a table using in my form_submit()
{
...
$myoutput = theme('table', $hdr, $rows);
return $myoutput; // does not do anything. so doing drupal_set_message($myoutput).
}

But the problem is that the output from the form_submit() function does not get displayed. Now I am using drupal_set_message($myoutput) to view the results. I am sure there must be a better/simpler way. Can you suggest something?

Thanks for any help.

PS: I would not want to use any special module for this. Not even CCK/views, if I can help it.

Comments

stone_d’s picture

the rules module provides you with the ability to receive variables from a form. This might be useful for what u need ... you may view content and - in your case - the form-variables

rajeshs.ind’s picture

Thanks for the reply.

My requirement is only to display the results of the form submit (not a one line mesg using drupal_set_message(), but display an array as html table ). Can the rules module help me with that?

I think that this is a very simple requirement: to see the results of a form action, which should be available in the core. I think I am missing something basic here.

da_solver’s picture

Hi,
I just asked the Leprechaun. He says you can issue a call to drupal_goto() in your submit handler.

function MYMODULE_myform_form_submit($form, &$form_state) {
    $query_string = 'first_name=';
    $query_string .= $form_state['values']['employee_first_name'];
    drupal_goto("my_report_page", $query_string);
}

So you are now passing the request parm "first_name=VALUE". So, in you report page you can simply get the value by using the expression $_REQUEST['first_name']

You use hook_menu to create a url to your report page. The url in the above example is "http://mysite/my_report_page". Here is the hook_menu implementation:

function MYMODULE_menu() {

  $items['my_report_page'] = array(
    'type' => MENU_CALLBACK,
    'title'=>'My Report', 
    'access arguments' => array('access content'),
    'page callback' => 'my_table_report',);
   return $items;
}

Hint! You need to use the admin performance function and clear cache before Drupal recognizes the new menu/url. Note the function that will be called is "my_table_report()".

Your report code would be something like:

function my_table_report() {
    $first_name = $_REQUEST['first_name'];
     if (!empty($first_name) {
         ..... database query here
          .....
     } else {
          .... create a table with an error message
     }
    return theme('table', $hdr, $rows);
}

You can also enhance the "menu callback" with "wild cards" and "load arguments" (e.g. my_report_page/%). However, a simple drupal_goto (which is just a redirect with the option to send query parameters) should suffice.

rfay’s picture

There are at least a couple of ways to do this:

1. Often the form will rebuild with the new information. To do this set $form_state['rebuild'] = TRUE in your submit function, and in the formbuilder function, build it with the new information.

2. Sometimes the form will redirect to another page using arguments to build. This is inappropriate for security reasons if it's changing the state on the server, but OK otherwise.

patrickd’s picture

Putting
$form_state['rebuild'] = TRUE
into my submit function works perfectly.

It re-renders the form directly without any page redirect and also keeps the last values.

Thanks!

Justincletus’s picture

Thanks For Your Useful Information.