Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

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) {
    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?

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

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.

share|improve this answer
add comment

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.

share|improve this answer
    
Actually, no. I tried this, but it didn't work because the #form_id were the same, so the first form was picked up. –  Florian Margaine Nov 15 '12 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 ? –  bobylapointe Nov 15 '12 at 21:10
    
Yes, but it's the product id of the first form. –  Florian Margaine Nov 15 '12 at 21:20
    
Weird, I do it very frequently. post your code. –  bobylapointe Nov 15 '12 at 21:32
    
Well, I'll try that tomorrow. It's late in there, I'll keep you in touch if I have time :) –  Florian Margaine Nov 15 '12 at 21:36
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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