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, &$form_state) {

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?

2 Answers 2


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 => array(
                'callback' => '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.


Hook_forms is an answers. There is a second way :

In your form, add in the #name of the submit, the $product_id;

like :

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('submit'),
    '#name' => $product_id,

Then, in the submit callaback, you'll find in the $form_state['triggering_element'], the product_id value. So even if you use the same form in a foreach loop, you can know witch 'submit' have been triggered.

  • Actually, no. I tried this, but it didn't work because the #form_id were the same, so the first form was picked up. Nov 15, 2012 at 21:03
  • What 'it didn't work' means ? are you able to get the product_id in the $form_state['riggering_element'] in the submit callback ? Nov 15, 2012 at 21:10
  • Yes, but it's the product id of the first form. Nov 15, 2012 at 21:20
  • Weird, I do it very frequently. post your code. Nov 15, 2012 at 21:32
  • Well, I'll try that tomorrow. It's late in there, I'll keep you in touch if I have time :) Nov 15, 2012 at 21:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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