Two forms on the same page - both submit the same I have two forms on the same page, both called with drupal_get_form('some_form_name', $product_id);. The $product_id variable is used to fill in a hidden input. (The code is in a foreach, the $product_id variable is different for every form.)

In the generated HTML, the forms are correctly built. Both have different IDs, and both hidden fields have the correct value.

However, in the submit function of this forms, with the following code:

function some_form_name_submit($form, &amp;$form_state) {
    dsm($form_state['values']['product_id']);
}


The product id is always the same. The first generated form's product id.

After some research, I found out that the hidden input form_id is the same in both forms. Looking into that.

Is there a way to solve this?
Found the solution. The problem was that the forms had the same form_id -- some_form_name, the string passed to drupal_get_form().

I generated the string with something like drupal_get_form('some_form_name_' . $product_id); instead of passing as second argument.

However, that means I would have to define a function for each product id. To avoid this, I implemented hook_forms(), so that each form id starting with some_form_name_ would use the same callback to generate the form:

function mymodule_forms($form_id) {
    if (preg_match('/^some_form_name_\d+$/', $form_id)) {
        return array(
            $form_id =&gt; array(
                'callback' =&gt; 'some_form_name',
            ),
        );
    }
}


This way, even though each form has a different form_id, they all use the some_form_name function to generate the form.

And no problem left.