您可以将数据保存在 cookie 中,该 cookie 将在登录页面后传递到页面。
假设您正在使用 Form API 创建表单,并且您在函数中有一个名为“fieldname”的字段来生成表单:
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:
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 后将其删除或设置较短的过期时间以便自动清理它们。