I have a problem, the form work fine once and then doesn't work when I logout and try again. I need to empty cache and try again to have it work again. I think this is something linked to the form_id not found in cache but I don't know how to fix it.

Watchdog error:
Invalid form POST data.

When Drupal's core caching feature is switched on, forms are delivered to anonymous visitors from cache which results in the situation where one and the same form gets delivered more than once with the same form_build_id.

As soon as one of the anonymous users submits such a form, the record gets removed from cache.

The next visitor who is submitting the same form will potentially run into trouble because of the missing form record in cache.

The regular Drupal FAPI seems to be OK with that but if the form gets submitted via Ajax, the submission gets rejected, a watchdog entry gets issued ('Invalid form POST data') and drupal_exit() gets called.

The result of that is that the user would have to reload the page before being able to submit the form. This can be annoying i.e. if the form being used is the user login form. It seems as if just nothing is happening.

This issue arrises always when page caching for anonymous users and form submission via Drupal's own ajax mechanism get used in combination.

Any solution?

Thanks

Comments

girishmuraly’s picture

Take a look at http://drupal.stackexchange.com/questions/36830/invalid-form-post-data-i... and http://drupal.org/node/1694574

I think you need to disable "cache pages for anonymous users" and perhaps have Varnish or another page cache mechanism instead..

dgtlmoon’s picture

disabling caching is probably unsuitable for most websites tho :(

subadmin’s picture

i need cache on my site, but problem still there, any solution ?

clecidor’s picture

Issue summary: View changes

After the "Invalid form POST data" watchdog message, notice that the ajax_get_form() function calls drupal_exit().

Your custom module can take advantage of this by hooking into hook_exit()... from there you can rebuild the expired/uncached form and attempt to resubmit form using Drupal's ajax_commands framework.

The following is an example of how I resolved this problem for an "AJAX-ified" user-login form.

/**
 * Implements hook_exit().
 */
function example_exit($destination = NULL) {
  if (arg(0) == 'system' && arg(1) == 'ajax') {
    $is_user_login_form_submission = isset($_POST) && isset($_POST['name']) && isset($_POST['pass']) && isset($_POST['form_build_id']);
    if ($is_user_login_form_submission && user_is_anonymous()) {
      $form_build_id = $_POST['form_build_id'];
      $form_state = form_state_defaults();
      $form_state['values'] = $_POST; // Important!
      $form = form_get_cache($form_build_id, $form_state);

      if (!$form) {
        watchdog(__FUNCTION__, 'User login AJAX form submission failed. Trying again...', array(), WATCHDOG_WARNING);

        $form = drupal_rebuild_form('user_login', $form_state);
        $form['#build_id_old'] = $form['#build_id']; // Important!

        // Try form submission again after it is rebuilt above
        $commands[] = ajax_command_update_build_id($form);
        $commands[] = ajax_command_invoke('form#user-login', 'trigger', array('submit'));

        print ajax_render($commands);
      }
    }
  }
}
Rosca Serghei’s picture

sudo a2enmod rewrite
sudo /etc/init.d/apache2 restart
cd /etc/apache2/mods-enabled
grep mod_rewrite *

tanmayk’s picture

Thanks @Shillen.

tbudz’s picture

I had to repair the cache_form table in mysql just clearing cache was not enough.

tannguyenhn’s picture

I got same problem with my custom form use form ajax api.
But I solve by solution https://www.drupal.org/node/1694574#comment-11526105.

stevo70’s picture

I know this is old, but #7 fixed my issue "Error: Invalid form POST data"

firewaller’s picture

FYI #4 seems to have a similar approach to the sandbox module suggested here: https://www.drupal.org/project/drupal/issues/1694574#comment-10938349

VikrantR’s picture

Thanks Clecidor, Point #4 works for me.