Multiple forms one page always submit with the values of the first I have got a form defined that is displayed multiple times on a single page. For each instance of the form, different values (node ids) are inserted into the hidden fields, which seems to be there once the form is rendered.

However, when I submit any of the forms on the page, the submit handler always gets the values that have been inserted for the first instance and I can't seem to get hold of why this is happening.

One thing that I noticed though is that all of my forms have the same form_id, form_token and only the form_build_id changes.

Any idea why this is happening, I can't find a solution to this problem anywhere.

Thanks a lot.

Code:

I'm attaching the form to the nodes in hook_node_view like this:

$node-&gt;processed['add_collection_form'] = drupal_get_form('add_to_collection_form', $node-&gt;nid);


when I try to do it in hook_node_load drupal always complains about the memory limit in PHP even though I have set it to a generous 1GB, which is pretty weird.

The form on the page are rendered the standard way:

print drupal_render($node-&gt;processed['add_collection_form']);


in node--.tpl.php
To use multiple instances of the same form on a page you need to implement hook_forms(), which allows you to essentially use 'aliases' for your forms.

function hook_forms($form_id, $args)
{
  $forms = array();

  // Check if the form id is of the pattern add_to_collection_form-[NUMBER]
  if(preg_match('/^add_to_collection_form-\d+$/', $form_id)
  {
    $forms[$form_id]['callback'] = 'add_to_collection_form';
  }

  return $forms;
}


The above code tells Drupal that if a form ID has the pattern add_to_collection_form-[NUMBER], to use the add_to_collection_form form callback.

Now you can call the forms on your page like this:

drupal_get_form('add_to_collection_form-1', $node-&gt;nid);
drupal_get_form('add_to_collection_form-2', $node-&gt;nid);
drupal_get_form('add_to_collection_form-3', $node-&gt;nid);