Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

drupal 7 Query database and display results after form submit in custom module

Tags:

drupal

function mymodule_search_form($form, &$form_state) {

  $form..... define the form here

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Search',
  );

  return $form;
}

function mymodule_search_form_submit($form, &$form_state) {

  //process the form and get result 
  $output = this is the result with a table of data.
  //I want to display the result table here.

 //Now I can only use drupal message to display on top.
 drupal_set_message($output);

  return;
}

So basically I want to have a form to search something from database. Once click the submit to search, and get the result.

I want to display the result just under the form in the same form page. Do not go to another page, just in the original form page.

The form can be clean/reset into original status, which is fine.

http://drupal.org/node/542646 This discussion is what I want, but there looks no solid result/solution there.

like image 544
Allen Chen Avatar asked Mar 24 '13 14:03

Allen Chen


1 Answers

You can stash the output table in the $form_state, set the form to rebuild, and display it if it exists in the original form function, e.g.

function mymodule_search_form($form, &$form_state) {

  $form..... define the form here

  if (!empty($form_state['results_table'])) {
    $form['results_table'] = array('#markup' => $form_state['results_table']);
  }

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Search',
  );

  return $form;
}

function mymodule_search_form_submit($form, &$form_state) {
  $form_state['results_table'] = function_to_get_table();
  $form_state['rebuild'] = TRUE;
}
like image 135
Clive Avatar answered Oct 02 '22 23:10

Clive