【发布时间】:2010-11-16 03:01:09
【问题描述】:
我有这个接受用户输入的表单。我喜欢做的是,基于该用户输入,我想检索数据并将其显示给用户。
到目前为止,我已经实现了hook_menu并注册了表单的相应url,并实现了提交按钮的“#submit”属性引用的提交函数。我还实现了数据检索代码,效果很好。
这是我的问题 - 我不知道如何显示检索到的数据。为了找到解决方案,我尝试了多种方法。
首先,使用主题功能,希望打印它的返回值来显示数据。其次,使用新注册的 url 设置表单数组的“#action”元素,因为我认为使用与表单相同的 url 只会导致 drupal 返回该表单而不是我的数据。因此,我创建了一个静态变量并将所有检索到的数据存储在其中;顺便说一下,这是在提交函数中完成的。当我在菜单回调中检查此变量时,未设置此变量。
总结一下我的问题,表单的访问url和表单提交不同,比如
Form url – http://....?q=mymodule/form
Submit url (value of ”#action”) – http://....?q=mymodule/execute
,我在提交函数中设置的静态变量的数据在菜单回调中不可用。如何使数据可用?
这是我的部分代码 -
static $retrieved_data;
function mymodule_menu() {
$command = array();
$command['mymodule/form'] = array(
'title' => 'user input',
'page callback' => 'response',
'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);
}
非常感谢
【问题讨论】: