Hello,

I'm writing a module who is about displaying a list of node.
First I've created a form, which contains a #AHAH properties in it.
I'm trying to display a list of node on the same page but nothing happens.

The question is : How do I show the results of my query after the form ?


function iew_autotextfields( &$form_state) {
[...]

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

function iew_autotextfields_submit($form, &$form_state) {
  if (!empty($form_state['iew_submission'])) {
    return;
  }
  // Continue to handle submit processing.
  $output='';
  $type="associations";
  $query = db_query("SELECT * FROM {node} WHERE type=%d", $type);
   while ($row=db_fetch_array($query)) {
   $output .= $row['title'];
   }
  if (isset($query)) {
    drupal_set_message(t('Search submitted successfully.'));
   }else { 
     drupal_set_message(t('There was an error. Please try again.'));
       }
return $output;
}

Comments

ivanliaw’s picture

I'm having the same issue as vallou17 did. I can print $output in drupal_set_message() function but nothing return in $output. And the only different is that I'm using Drupal 7.x.


drupal_set_message($output);   //displayed on screen but in message box


return $output;  //nothing show up

Does anyone know the answer? Or should I write the db_select() somewhere else instead of in form_submit()? Thanks in advance.

andy tawse’s picture

Yeah I'd redirect somewhere else using $form_state['redirect'] (I think that's right), then show whatever you want on that page.

ivanliaw’s picture

Hi, Andy,

Thank you for your prompt reply. I had tried $form_state['redirect'] as you suggested. And it works but I found that I can only pass the query parameters to the redirected form by using the URL like below.
For example, to redirect to 'my_form?type=page':

  ...
  function iew_autotextfields_submit($form, &$form_state) {
    if (!empty($form_state['iew_submission'])) {
      return;
    }

    $node_type = $form_state['values']['type']; 

    $form_state['redirect'] = array( // is 'redirect' the only way to forward to a new form or even forward to this form itself?
      'my_form',
      array(  
        'query' => array(
          'node_type' => $node_type,
        ),
      ),
    );
  }

  function my_form(){
    ...
    $node_type = $_GET['node_type']; // is here any other workaround?
   
    $query = db_query("SELECT * FROM {node} WHERE type=%d", $node_type);
    while ($row=db_fetch_array($query)) {
      $output .= $row['title'];
    }
    print $output;
  }
}
  

I found a similar issue in http://drupal.org/node/542646. But the sample code provided by quiet is not working.
Is there any other way to pass the parameters with 'post' rather than 'get'? Is 'redirect' with 'get' is the only way we can implement this?

nevets’s picture

You can get rid of the submit function, in which case control will return to the form. Since that function has access to $form_state, you can check that to see if there are submitted values.

ivanliaw’s picture

Hi, nevets,

Thank you for your reply. I did get rid of submit function as you suggested and the submitted values can be pass to the same form. But the query result cannot be printed out. Where did I do wrong? Below is the module that I wrote. Please help me to review it give me some advice. Thanks again.

<?php

/**
* Implements hook_menu().
*/
function date_query_menu() {
  $items['date_query'] = array(
    'title' => 'Report',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('date_query_form'),
    'access callback' => TRUE,
    'type' => MENU_CALLBACK
  );
  return $items;
}

/**
* Define a form.
*/
function date_query_form($form, &$form_state) {
	
  $form['date_query'] = array(
    '#title' => t(''),
    '#type' => 'fieldset',
    '#description' => t('Enter query condition:')
  );
  $form['date_query']['start_date'] = array(
    '#title' => t('Start date:'),
    '#type' => 'textfield',
    
  );
  $form['date_query']['end_date'] = array(
    '#title' => t('End date:'),
    '#type' => 'textfield',
    
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit')
  );
  
  if (isset($_POST['start_date']) && isset($_POST['end_date'])){
  	
    $form_state['rebuild'] = TRUE;
    $start_date = $_POST['start_date'];
    $end_date = $_POST['end_date'];
    
    $query_result = db_query_with_date($start_date, $end_date);
    
    //drupal_set_message($query_result); //the result can be printed in message box
    
    $form['query_result'] = array(
      '#markup' => $query_result,   //nothing print out 
    );
    
  }
  return $form ;
}

function db_query_with_date($start_date, $end_date){

  $query = db_select('node', 'n')->extend('PagerDefault');
  $query
    ->condition('type', 'page')
    ->condition('created', array(strtotime($start_date), strtotime('+1 day',strtotime($end_date))), 'BETWEEN')
    ->fields('n', array('nid', 'title', 'created'));
    //->range(0,100);
    
  $results = $query->execute();
  
  $header = array(
      'Node ID','Title', 'Created',
  );
  $rtnStr = '';
  
  $rows = array();
  //$DBdata = array();
  foreach ($results as $node) {
	  $rows[] = array(
	    $node -> nid,
			$node -> title,
			readabledateformat($node -> created),
	  );
	}
	// Theme the html table
  $html = theme('table', 
	  array(
	  	'header' => $header,
	  	'rows'=>$rows,
	  )
  );
  
  //Append pager
  $html .= theme('pager',
	  array(
		  'tags' => array()
	   )
  );
  
  return $html;

}

function readabledateformat($enterdate) {
  //$newdate = date("D, j M Y", $enterdate);
  $newdate = date("Y/m/d", $enterdate);
  return $newdate;
}

?>


nevets’s picture

Try removing $form_state['rebuild'] = TRUE;

ivanliaw’s picture

The result is the same after removing $form_state['rebuild'].