I created a custom module which contains a form with submit button. After connecting to the data base I have an output table that contain the result.

The question is that I can't display and show this table($output) in the main content page

I used drupal_set_message($output); in

But I want to display the result in the body of the page.

function form_test_menu() { $items['formtest'] = array( 'title' => 'Form test', 'access arguments' => array('access content'), 'page callback' => 'drupal_get_form', 'page arguments' =>array('form_test_form'), 'access callback' => TRUE, ); return $items; } function form_test_form($form,&$form_submit) {

$form = array(); $form['#multistep'] = TRUE;

$form['search'] = array( '#title' =>t('search'), '#type' => 'textfield', // '#required' => TRUE, ); $form['fromdate'] = array( '#type' => 'date_popup', '#date_format' => 'Y-m-d', '#title' => "from " ); $form['todate'] = array( '#title' => t('to'), '#type' => 'date_popup', '#date_format' => 'Y-m-d', ); $form['submit'] = array( '#value' => 'Submit', '#type' => 'submit',


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

} return $form; } function form_test_form_submit($form, &$form_state) { drupal_set_message(t('in')); $form_state['storage']['values'] = $form_state['values'] ; $search = $form_state['values']['search']; drupal_set_message($search); $todt=$form_state['values']['todate']; $begindt=$form_state['values']['fromdate']; $search_terms = explode(' ', $search); drupal_set_message($todt); db_set_active('my_other_db'); $query = db_select('karar', 'f'); if (empty($search)&!empty($todt) &!empty($begindt)){

$query ->fields('f', array('type','subject','date')) ->condition('f.date',$todt,'<=') ->condition('f.date',$begindt,'>=');


else if (empty($todt) & empty ($begindt) & !empty($search)){ foreach($search_terms as $search_term){ $query->condition('f.subject','%'. db_like($search_term) . '%', 'LIKE') ->fields('f', array('type', 'subject','date')); drupal_set_message($search_term);

} } else if (empty($todt) & !empty ($begindt) & ! empty ($search)){



foreach($search_terms as $search_term){ $query->condition('f.subject','%'. db_like($search_term) . '%', 'LIKE')
->fields('f', array('type', 'subject','date')); drupal_set_message($search_term); } drupal_set_message("begin + kelmeh w ana e5deh el last today");
} else if (!empty ($search) & !empty ($todt) & !empty($begindt)){ $query ->condition('f.date',$todt,'<=') ->condition('f.date',$begindt,'>=') ->fields('f', array('type', 'subject','date')); foreach($search_terms as $search_term){ $query->condition('f.subject','%'. db_like($search_term) . '%', 'LIKE');
drupal_set_message($search_term); }


else { db_set_active(); form_set_error('keys', t('Please enter some keywords.')); drupal_set_message(t(''),'error'); return; } drupal_set_message(t('after return')); $result = $query->execute();

  $header = array('year', 'subj','hist');
  $rows = array();

  while($data = $result->fetchObject()){
         $rows[] = array(
  $output = theme_table(
          "header" => $header,
          "rows" => $rows,
          "attributes" => array(),
          "sticky" => true, // Table header will be sticky
          "caption" => "",
          "colgroups" => array(),
          "empty" => t("Table has no data!") // The message to be displayed if table is empty

    $variables = array('header' => $header, 'rows' => $rows);
$content = theme('table', $variables, array());
 $form_state['results_table'] = $output;
  $form_state['rebuild'] = TRUE;
  return $form;


but nothing is displayed any help

  • what do you mean by main content page? – rfegade Oct 13 '15 at 10:55
  • There is two ways to display it in content area: 1) If you created the form from the block then please assign it in the region "main content" 2) If you created this from custom page means hook_menu, then see your page.tpl.php structure whether the "main content" region is assign in the right place. – user50292 Oct 13 '15 at 10:56
  • Welcome to Drupal Answers! Since the question is about code, we would need to see it. Plus, we cannot write the code for a form submission handler to show its output, nor we cannot describe you exactly how to do it; the topic is too broad, also because we don't know what you understand about Drupal form API. – kiamlaluno Oct 13 '15 at 11:50
  • i create a module that read from database and display the result in a TABLE. I Am not able to display the table – hiba najjar Oct 15 '15 at 5:37
  • You can edit the template.php in your themes folder ( sites/all/themes/YOUR_CUSTOM_TEMPLATE ) and in the YOUR_CUSTOM_TEMPLATE_preprocess_function´ add the call to your module function, like this: $variables["page"]["content"]= your_function()` . If you need your result to appear only in a node, then you can previously identify your node by its nid: if ($variables["node"]->nid == YOUR_NID ) { $variables["page"]["content"]= your_function() } – Cesar Oct 15 '15 at 6:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.