views:

484

answers:

1

I have a form that displays two submit buttons. The first submit button sets $form_state['storage'] to a value. The second submit button then reads this $form_state['storage'] value. If the value is set, then a success message is displayed. If the value is not set, then a fail message is displayed.

Here is the code that will reproduce my issue:

function mymodule_test_admin() {
    return drupal_get_form('mymodule_test_form');
}

function mymodule_test_form(&$form_state) {
    $form['mymodule_test_form1'] = array(
        '#type' => 'fieldset',
        '#title' => t('test 1'),
        '#collapsible' => TRUE,
        '#collapsed' => FALSE,
        '#tree' => TRUE
    );

    $form['mymodule_test_form1']['submit'] = array(
        '#type' => 'submit',
        '#value' => t('button 1'),
        '#submit' => array('mymodule_test_form1_submit')
    );

    $form['mymodule_test_form2'] = array(
        '#type' => 'fieldset',
        '#title' => t('test 2'),
        '#collapsible' => TRUE,
        '#collapsed' => FALSE,
        '#tree' => TRUE
    );

    $form['mymodule_test_form2']['submit'] = array(
        '#type' => 'submit',
        '#value' => t('button 2'),
        '#submit' => array('mymodule_test_form2_submit')
    );

    return $form;
}

function mymodule_test_form1_submit($form, &$form_state) {
    $form_state['storage']['test_1'] = 'test 1';
    drupal_set_message(t('@message', array('@message' => $form_state['storage']['test_1'])));
}

function mymodule_test_form2_submit($form, &$form_state) {
    if (isset($form_state['storage']['test_1'])) {
        drupal_set_message(t('success'));
    }    else {
            drupal_set_message(t('fail!'));
        }
}

When you click the first submit button, $form_state['storage'] is properly set. When you click the second submit button, the message "success" is displayed. So far so good. Now do a page refresh. The message "fail!" is displayed.

So everything works right up until the page refresh. The page refresh essentially is only calling the second submit function. In theory, $form_state['storage'] should still be populated and the message displayed should be "success". However, taking a look at the $form_state dump shows $form_state['storage'] is NULL after the page refresh. I can't tell if my code logic is wrong or if $form_state['storage'] is being cleared on the page refresh.

Any ideas?

Thanks for your help.

+1  A: 

Storage after submit will cleared, use $_SESSION['mymodule_test_XXX'] for storing in multistep forms...

Nikit