Hi,

I am using a multistep form in a Drupal 6 module. The first time I generate the form I get values for form elements , form_id like this:





But when I resubmit the form I get different values for the form element ids :





As you will notice all the elements' ids become 'edit-xxxx-1' to 'edit-xxxx' .

This is causing lot of confusion as I am not able to access the elements using javascript when I need.

Basically this form shows a list from a table. A 'show next' submit button show the next elements - redirects the form to 'step 1' again.

or a 'delete' submit button taking it to next step where the list of entries are deleted.

Is there any way to pass on form-id so that the form elements will have same ids.

Thanks
Karthik

Comments

ponkarthik’s picture

Hi everyone,

Sorry for the double post. But the code didnot display in the previous post..

I am using a multistep form in a Drupal 6 module. The first time I generate the form I get values for form elements , form_id like this:

<input type="text" maxlength="10" name="end_date" id="edit-end-date-1" size="10" value="01/01/2030"  class="form-text" />
<input type="submit" name="op" id="edit-submit-1" value="Delete"  class="form-submit" />
<input type="hidden" name="form_build_id" id="form-47126414ffa9ca562649c147be56f8d5" value="form-47126414ffa9ca562649c147be56f8d5"  />
<input type="hidden" name="form_token" id="edit-alogbook-formz-form-token-1" value="73eed4b9915bdc16f86cbb497c41b48b"  />
<input type="hidden" name="form_id" id="edit-alogbook-formz-1" value="alogbook_formz"  />

But when I resubmit the form I get different values for the form element ids :

<input type="text" maxlength="10" name="end_date" id="edit-end-date" size="10" value="01/01/2030"  class="form-text" />
<input type="hidden" name="form_build_id" id="form-6149df05f231c641a14ed5e91055c9a2" value="form-6149df05f231c641a14ed5e91055c9a2"  />
<input type="hidden" name="form_token" id="edit-alogbook-formz-form-token" value="73eed4b9915bdc16f86cbb497c41b48b"  />
<input type="hidden" name="form_id" id="edit-alogbook-formz" value="alogbook_formz"  />
<input type="submit" name="op" id="edit-submit" value="Delete"  class="form-submit" />

As you will notice all the elements' ids become 'edit-xxxx-1' to 'edit-xxxx' .

This is causing lot of confusion as I am not able to access the elements using javascript when I need.

Basically this form shows a list from a table. A 'show next' submit button show the next elements - redirects the form to 'step 1' again.

or a 'delete' submit button taking it to next step where the list of entries are deleted.

Is there any way to pass on form-id so that the form elements will have same ids.

Thanks
Karthik

jbjaaz’s picture

Hi Karthik, did you ever figure out why this was happening. It currently has me stumped.

====

In case anyone happens across this, I might have figured it out. Not sure if my case matches what Karthik was doing, though.

In my case, I have a custom module which produces a form and then via the nodeapi hook, embeds that form into a specific node.

if ($op == 'view' && $node->nid == #) {
  $node->content['body']['#value'] .= drupal_get_form('myformid');
}

What I discovered was that the "view" hook was being called twice. I really have no idea why it's called twice. But, by adding a print_r($node) to the above code, I noticed that in the first pass, all form element ID's were in the form of edit-nameofelement and then in the second pass, the ID's changed to edit-nameofelement-1.

Again, I'm not sure why that is happening. Maybe some Drupal guru will stumble upon here and drop some knowledge on us.

Anyway, to solve the issue, I utilized the "load" operation to add a new property to the node object which contained the form. Then in the "view" operation, I added the form to the body.

if ($node->nid == #) {
    switch ($op) {
      case 'load':
        $node->myform = drupal_get_form('myform');
        break;
      
      case 'view':
        if (isset($node->myform)) {
          $node->content['body']['#value'] .= $node->myform;  
        }
        break;
    } 
}

The above code results in a form with ID's that don't have an extra number appended at the end.

steveoliver’s picture

3 years later, and I'm running into this issue when running my form through drupal_execute().

form_clean_id(), which is called in various places through the drupal_*_form() functions, is responsible for incrementing these form ids.

I'm on the hunt for a way to process my form more than one time using drupal_get_form and then drupal_execute...

I'm probably not doing things the right way...

Don't expect this to help anyone at the moment--just leaving some breadcrumbs...

-Steve