【问题标题】:Drupal multi-form loses state on page refreshDrupal 多表单在页面刷新时丢失状态
【发布时间】:2011-12-15 12:45:40
【问题描述】:

我有一个多页表单。访问第 1 页,第 2 页,然后访问第 3 页。按刷新 (f5),表单返回第 2 页。

这适用于 drupal-6。问题看起来类似于http://drupal.org/node/1060290

通过 form_cache 数据库表深入研究问题。第 1 页和第 2 页的数据都出现在那里。在 php 调试器中,它看起来好像已经创建了一个新的 form_id。 IE。 storage_form-1add3819cbea88139679819935a69686 是数据库缓存表中的键,form-b​​cf9556f57f5352a57dfbba4c2120ee7 是刷新时的“form_id”。

我的表单代码是什么样的?

主窗体函数:

function myform_online(&$form_state) {
  // $form_state['storage']['step'] keeps track of what page we're on.
  // start at step 1 if no storage has been set
  if (!isset($form_state['storage']['step'])) {
    $form_state['storage']['step'] = 1;
 }

 // If we are saving the form data we should submit rather than display the details.
 // At least look at saving the step.

 // Don't lose our old data when returning to a page with data already typed in.
 $default_values = array();
 if (isset($form_state['storage']['values'][$form_state['storage']['step']])) {
    $default_values = $form_state['storage']['values'][$form_state['storage']['step']];
 }

 switch ($form_state['storage']['step']) {
    case 1:
        // Your Details
        module_load_include('inc', 'join_online', 'includes/step1'); 

我们处理提交:

function join_online_submit($form, &$form_state) {
//Save the values for the current step into the storage array.
//dsm($form_state); 

$form_state['storage']['values'][$form_state['storage']['step']] = $form_state['values'];

# ahah - bail.
if ($form_state['ahah_submission']) {
    return;
}

// How do we work out if this was a refresh? It currently does start with 1 and think that the step is #2. 

//Check the button that was clicked and change the step.
if ($form_state['clicked_button']['#id'] == 'edit-previous') {
    $form_state['storage']['step']--;
} elseif ($form_state['clicked_button']['#id'] == 'edit-next') {
    $form_state['storage']['step']++;
} elseif ($form_state['clicked_button']['#id'] == 'edit-finish') {
    //You should store the values from the form in the database here.
    //We must do this or the form will rebuild instead of refreshing.
    unset($form_state['storage']);

    //Go to this page after completing the form.
    $form_state['redirect'] = 'join_online/form/thank-you';
}
}

【问题讨论】:

  • 您有#ajax#ahah 元素吗?因为在这种情况下,情况有些不同。你可能想看看这里:drupal.org/node/650016
  • 这是一个好点。失败的上一页是#ahah。 Drupal 执行“新缓存”,然后删除旧缓存。但是在这种情况下刷新时没有指向新缓存的指针。也找不到地方把它放进SESSION。在构建表单之前不会缓存,然后你得到的只是一个字符串。还在寻找。
  • 我认为您不必求助于会话来保存数据。最好的办法是找到一个带有#ahah 元素的多步骤表单的良好工作示例,然后从那里开始。不幸的是,我想不出一个,但那里必须有一个模块。这是一篇很不错的文章,显然有人在与你一样的事情上苦苦挣扎:designend.net/en/…
  • 也许您可以使用多步模块。我认为它会实现您的目标以及更多。

标签: php forms drupal drupal-6


【解决方案1】:

如果您在 _submit 函数中使用 $form_state['rebuild'] = TRUE ,则​​会保存表单的状态并可用于默认值。

检查这个例子: http://www.ferolen.com/blog/how-to-create-multistep-form-in-drupal-6-tutorial/

【讨论】:

    猜你喜欢
    • 2019-02-09
    • 2018-11-17
    • 2020-01-20
    • 2020-04-30
    • 2019-09-12
    • 1970-01-01
    • 1970-01-01
    • 2018-09-17
    • 2017-05-24
    相关资源
    最近更新 更多