您可以将数据保存在cookie中,该cookie将在登录页面后传递给页面。
假设你正在使用的表单API来创建表单,而且必须生成形式的功能叫做“字段名”字段:
function my_form() {
$form['fieldname'] = array (
'#type' => 'textfield',
'#title' => 'Some fieldname'
);
// Second field if you need to save another
$form['fieldname2'] = array (
'#type' => 'textfield',
'#title' => 'Some other fieldname'
);
}
在设置cookie的(S)提交处理您的形式:
function my_form_submit($form, &$form_state) {
// Set the cookie with their data before they are redirected to login
// Use the array syntax if you have one than one related cookie to save,
// otherwise just use setcookie("fieldname",...
setcookie("saved_data[fieldname]", $form_state['values']['fieldname']);
// Your other code
}
他们登录并重定向到您的窗体的第二页后,就可以读取cookie(一个或多个)的值,如果存在的话,将其插入的默认值字段:
function my_form() {
if (isset($_COOKIE['saved_data[fieldname]'])) {
$default_fieldname = $_COOKIE['saved_data[fieldname]'];
}
else {
$default_fieldname = '';
}
// Same check for the second field
if (isset($_COOKIE['saved_data[fieldname2]']))...
$form['fieldname'] = array (
'#type' => 'textfield',
'#title' => 'Some fieldname',
'#default_value' => $default_fieldname
);
// Second field if used
$form['fieldname2'] = array (
'#type' => 'textfield',
'#title' => 'Some other fieldname',
'#default_value' => $default_fieldname2
);
}
Cookie设置完成后会在每次加载页面时显示,因此如果有多次失败的登录尝试或其他页面浏览次数不重要。您可能应该在使用后删除Cookie,或设置一个短的到期时间,以便自动清理它们。
但是,如何触发Drupal中的保存和检索? – 2010-04-19 14:43:57
谢谢。这是一个很好的答案! – 2010-04-20 14:53:30
没问题。但我会第一个承认,在我想出这样一个解决方法之后,我会找到一个Drupal对象(或者数据库表或函数),它完全符合我的需求。祝你好运! – flamingLogos 2010-04-20 16:08:39