Hi. I was looking for an answer in the forums history, found only unanswered questions to this (like here: http://drupal.org/node/464960).

Why does this not work well and what should I do to make it work?

What it does: Display a button with "0++" which increments on submission to "1++" etc.
The issue: The form goes *nuts* on refreshing the 3rd and further pages (2++, 3++...). You are taken to 1++, but the problem is, that the submit handler is fired with *nutty* data in $form_state.

//The builder shows a button with "0++". The number comes from form_state['storage'] which gets populated in the submit handler
function _test_module_form($form_state) {
  $num = (int)$form_state['storage']['number'];
  $form['Next'] = array(
    '#type' => 'submit',
    '#value' => $num.'++',
  );
  return $form;
}

//submit handler increments $form_state['storage']['number']
function _test_module_form_submit($form, &$form_state) {
  if ($form_state['clicked_button']['#id'] == 'edit-Next')
    $form_state['storage']['number']++;
  //$form_state['rebuild'] is set automatically when ['storage'] is populated... I hope, see http://drupal.org/node/144132#form-state
}

A more complex example can be seen here: http://drupal.org/node/464960#comment-4068104

I'll be glad for any help and ideas. Thanks, RGB

Comments

operations’s picture

As stated here: http://api.drupal.org/api/drupal/includes--form.inc/function/drupal_buil...

the value of $form_state['rebuild'] must be set to TRUE so that the form rebuilds itself instead of refreshing. I tried it though but the same problem still exists.

Please if you have found any solution to this, let me know.

BayerMeister’s picture

I am referring to D6. There you have to set $form_state['rebuild'] to TRUE or populate $form_state['storage'] (it sets rebuild to TRUE as well).