开发者

How do i get the data?(Drupal 6.x)

I have this form which accepts user’s input. What I like to do is, base on that user input, I’d like to retrieve data and display it back to user.

So far, I have implemented hook_menu and registered respective url of the form, and implemented a submit function referred by “#submit” attribute of submit button. I’ve also implemented data retrieval code and works great.

Here’s my problem – I don’t know how to display retrieved data. I’ve tried several approaches in an attempt to find the solution.

First, with theme function, hoping that printing the return value of it would display the data. Second, setting “#action” element of form array with newly registered url, as I thought using the same url as form would only cause drupal to return that form instead and not my data. So, I creates a static variable and stores all the retrieved data in it;this is done inside submit function by the way. When I checked this variable inside menu callback, this variable is not set.

To summarize my problem, form has different access url than form submit, such as

Form url – http://....?q=mymodule/form
Submit url (value of ”#action”) – http://....?q=mymodule/execute

, and the data I’ve set inside submit function to static variable is not available in menu callback. How do I make the data available?

Here’s part of my code -

static $retrieved_data;

function mymodule_menu() {
   $command = array();

   $command['mymodule/form'] = array(
      'title' => 'user input',
      'page callback' => 'response',
   开发者_StackOverflow   'page arguments' => array('form'),
      'access arguments' => array('access content'),
      'type' => MENU_CALLBACK,
   );

   $command['mymodule/execute'] = array(
      'title' => 'Search',
      'page callback' => 'response',
      'page arguments' => array('execute'),
      'access arguments' => array('access content'),
      'type' => MENU_CALLBACK,
   );

   return $command;
}

function _response($paRequest){

   switch($paRequest){
      case "form":
         return drupal_get_form("_myform");
      break;

      case "execute":
         return $retrieved_data;
      break;
   }
}

function _myform(&$form_state) {
   $form['#action'] = url($base_path)."?mymodule/execute";
   .....
   $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Submit'),
      '#submit' => array('_data_retrieve'),    
   );
   return $form;
 }

function _data_retrieve($form, &$form_state){
   /*data retrieval code*/
   ........................
   $retrieved_data = db_fetch_object($result);
}

Thanks a bunch


Your method seems a bit complicated there. When I make systems with a form, I tend to do it this way. In your MYMODULE_menu() I would change the 'page arguments' => array('form'), to 'page arguments' => array('NAME_OF_FORM_FUNCTION'), where NAME_OF_FORM_FUNCTION would be _myform in this case. I would rename it to MYMODULE_MYFORMNAME.

then create a function:

MYMODULE_MYFORMNAME_SUBMIT($form, &$state) {
    // Enter code here to save the data from the form that is stored in $state
    // to the database with an SQL query or a node_save($node) if you are 
    // creating a node.
}

After that you can retrieve the data from the database in your _data_retrieve function and call that on the page where you want to retrieve the data.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜