4

I have a simple form inside a block:

function mymodule_block_view($delta = '') {
      switch($delta) {
        case 'some_name':

          $block['subject'] = t('title');
          $block['content'] = drupal_get_form('mymodule_form');
      }
      return $block;
    }

When I submit the form, the mymodule_form() method gets called twice. In the first run the values I have put into the textfields are correctly stored in $form_state. In the second run, they are not there anymore. Does anyone have an idea why this is the case?

What I would like to do is very simple: I'd like to retain the values I have put into the textfields, so when the form gets submitted, the page gets reloaded, and the values I gave are the new default_values of the form. I know how to do this in the code, but as I said, the form_state is empty in the second run, thus the form takes the 'default' default_values.

Why does mymodule_form() get called twice?

  • Perhaps you're confusing default_value and value here don't you think ? – Countzero Feb 17 '12 at 11:00
9

Drupal redirects after form submissions. So you are effectively making two requests, one for the form submission and one as a consequence of the redirect.

See Wikipedia: Post/Redirect/Get for details about why Drupal redirects after form submissions.

Set $form_state['redirect'] = false to prevent redirection.

  • Thanks for the great answer, that was exactly the "problem". I see the use of the RPG pattern, but in this specific case its okay to not use it, since the form is not saving any data, but is only used to change some content on the front end on the fly. Without redirect it works as expected (I added the line to the submit method of the form). – dschenk Feb 17 '12 at 13:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.