How to return the result to the module page?

cgs - July 12, 2007 - 11:57

I write a module (it is not node type), the module does some information process according to the user input. now, I want to return the results of information processing to the module pages, We could use the drupal_set_message to return some state information to the page, such as :

/**
* Handle form submission.
*/
function my_form_submit($form_id, $form_values) {
$input = $form_values['input'];
$result = my_handle($input);
if ($result == NULL) {
drupal_set_message(t('Timeout.'));
}
else {
drupal_set_message(t($result); // $result has much information.
}
}

But now I have much information ($result) to return, the drupal_set_message seems to be unable to return much information.
Does anyone have any suggestions on how to return so much information to the module page ? Thanks.

just an example

AjK - July 12, 2007 - 12:08

There are various ways but a common approach is this:

You process the user input in some way, often storing stuff into the database with a unique id (may be a node?) Who knows, thats down to you.

Then, when you exit the submit function return a redirect (the return value of a _submit function is a Drupal path).

eg: return 'mymodule/'. $nid;

(remember what you return from a _submit() function is a Drupal path to goto).

Now, define using hook_menu() you new path: "mymodule" and have it callback a "display" type page. for eaxmple in hook_menu():

<?php
      $items
[] = array(
       
'path' => 'mymodule',
       
'access' => 1,
       
'callback' => 'mymodule_display_results',
       
'type' => MENU_CALLBACK,
      );
?>

now define the display function

<?php
function mymodule_display_results() {
 
$args = func_get_args();

  if (!isset(
$args[0]) || !is_numeric($args[0])) {
   
drupal_not_found();
    return;
  }

 
// Assuming you used a node.....
 
$node = node_load($args[0]);

 
// Else get from the db the results you stored in the submit function...

  // .... go on to display what ever.
 
return theme('mymodule_display_results', $node);
}

function
theme_mymodule_display_results($node) {

 
// Create $output based on how you want to display it.
 
return $output;
}
?>

One last tip. When stuffing things into the database or when creating the $output you return to the browser always use the guidelines found here http://drupal.org/writing-secure-code

This is OK if I want to

apsivam - July 12, 2007 - 12:21

This is OK if I want to create a node or something similar. But how can I print some output if I'm not dealing with nodes at all. For instance take google_pr or whois module as an example, they take domain name as input and print some output without need for node. In my opinion (I may be wrong) both these modules not developed properly. I cannot use captcha with them.

--
Cheers,
Sivanandhan, P. (a.k.a. apsivam)

My example assumed that

AjK - July 12, 2007 - 12:32

My example assumed that maybe you using nodes. If you are writing a module you may well have your own tables in the database. That being the case it's up to you to write the relevent code, the sample above was just that, a sample.

Regarding other modules and input to them. If you want to "grab" the user input for them use hook_form_alter() to chain in your own submit handler and then you get the values :)

As for using captcha, that's another story altogether. It maybe that you can form_alter in but that's out of the scope of the original posters question so I'm not going down that road. maybe you should post some support requests to teh modules concerned.

I am not using nodes

cgs - July 12, 2007 - 13:11

Thank you for your reply, I am not using nodes , it is just a module which similar to the google_pr or whois module, I can get the user input use _form_submit($form_id, $form_values), but I don't know how to print much output information to the module page.

I am trying the hook_form_alter() now.

"Module Page"?

dman - July 12, 2007 - 13:46

Assuming that when you say 'module page' you mean a callback handler triggered through hook_menu ...

either

<?php
print theme('page',$result);
exit;
?>

or

<?php
return $result;
?>

is all you need in the handler.

OR, you can direct your form with an #action set to another callback (results) page
$form->'#action' => url('admin/your/module');
which does something new with the data.

.dan.
How to troubleshoot Drupal | http://www.coders.co.nz/

My module is like this, May be it is simple

cgs - July 13, 2007 - 12:36

yes,I have a callback handler triggered through hook_menu , my module really like this:

<?php
function mymodule_menu($may_cache) {
 
$items = array();
  if (
$may_cache) {
   
$items[] = array(
     
'path' => 'mymodule',
     
'access' => 1,
     
'callback' => 'mymodule_page',
     
'type' => MENU_CALLBACK,
    );
  }
  return
$items;
}

function
mymodule_page() {
 
// Return the HTML generated from the $form data structure.
 
$output = drupal_get_form('mymodule_form');
  return
$output;
}

function
mymodule_form() {
 
$form['url'] = array(
   
'#title' => t('URL'),
   
'#type' => 'textfield',
   
'#required' => TRUE,
  ); 
  
 
$form['submit'] = array(
   
'#type' => 'submit',
   
'#value' => t('Submit')
  );
  return
$form;
}

function
mymodule_form_submit($form_id, $form_values) {
  
$input = $form_values['url'];

 
// here , I do some process according to the $input.
  
$result = my_handle($input);
  
   if (
$result == NULL) {
      
drupal_set_message(t('error .'));
   }
   else {
  
// $result has much information. so I cant't return the information to page using drupal_set_message
     
drupal_set_message(t($result);
   }

  
//if I direct my form to another results page which has a callback handler, but how do I pass the $result to the callback handler of results page, when user press the sumit ,drupal will jump to mymodule_form_submit, but outside mymodule_form_submit, the $_POST['url'] become NULL.
  
return 'mymodule/result';
}
?>

My problem may be simple , but I have tried some methods . Thanks for all you replies.

I found the answer!

lhorthy - December 18, 2007 - 11:15

There is a great example on the Lullabot site using multi-part forms:

http://www.lullabot.com/articles/drupal_5_making_forms_that_display_thei...

Cheers,

Leonard

 
 

Drupal is a registered trademark of Dries Buytaert.