gpt4 book ai didi

drupal - 如何在 Drupal 中的 "access denied"页面上持久保存表单数据?

转载 作者:行者123 更新时间:2023-12-02 20:11:28 24 4
gpt4 key购买 nike

我们正在构建一个小型子网站,该网站在首页上有一个用户可以提交的输入框表单。从那里,他们被带到一个页面,其中包含更多关联的表单字段(添加更多详细信息、标记等),并且第一个主表单字段已填写。到目前为止,效果非常好。

问题出现在未登录的用户身上。当他们提交第一个表单时,他们会被带到允许他们登录的(基于 LoginToboggan 的)登录页面。他们登录后,重定向到第二个表单页面,但第一个主表单字段未填写 - 换句话说,表单数据没有保留。

我们如何存储该数据并使其在访问被拒绝的页面上持续存在?

最佳答案

您可以将数据保存在 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 将在每次页面加载时显示,因此,即使中间有多次失败的登录尝试或其他页面 View 也没关系。您可能应该在使用 cookie 后将其删除,或者设置较短的过期时间,以便自动清除它们。

关于drupal - 如何在 Drupal 中的 "access denied"页面上持久保存表单数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2625574/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com