Add multiple fields in +Add more button causes problem in sequence I am implementing a form and in that form it requires that two form element should be coming under one Add more button, so user can add any number of fields.

Here is the code which i am using to create a form.

function ajax_example_add_more($form, &amp;$form_state, $no_js_use = FALSE) {
  $form['description'] = array(
    '#markup' =&gt; '&lt;div&gt;' . t('This example shows an add-more and a remove-last button. The &lt;a href="!ajax"&gt;AJAX version&lt;/a&gt; does it without page reloads; the &lt;a href="!multistep"&gt;non-js version&lt;/a&gt; is the same code but simulates a non-javascript environment, showing it with page reloads.',
      array('!ajax' =&gt; url('examples/ajax_example/add_more'), '!multistep' =&gt; url('examples/ajax_example/add_more_no_js')))
    . '&lt;/div&gt;',
  );

  $form['#tree'] = TRUE;
  $form['names_fieldset'] = array(
    '#type' =&gt; 'fieldset',
    '#title' =&gt; t('People coming to the picnic'),
    '#prefix' =&gt; '&lt;div id="names-fieldset-wrapper"&gt;',
    '#suffix' =&gt; '&lt;/div&gt;',
  );
  if (empty($form_state['num_names'])) {
    $form_state['num_names'] = 1;
  }
  for ($i = 0; $i &lt; $form_state['num_names']; $i++) {
    $form['names_fieldset']['name'][$i] = array(
      '#type' =&gt; 'textfield',
      '#title' =&gt; t('Name'),
    );
    $form['names_fieldset']['surname'][$i] = array(
            '#type' =&gt; 'textfield',
            '#title' =&gt; t('SurName'),
    );
  }
  $form['names_fieldset']['add_name'] = array(
    '#type' =&gt; 'submit',
    '#value' =&gt; t('Add one more'),
    '#submit' =&gt; array('ajax_example_add_more_add_one'),
    '#ajax' =&gt; array(
      'callback' =&gt; 'ajax_example_add_more_callback',
      'wrapper' =&gt; 'names-fieldset-wrapper',
    ),
  );
  if ($form_state['num_names'] &gt; 1) {
    $form['names_fieldset']['remove_name'] = array(
      '#type' =&gt; 'submit',
      '#value' =&gt; t('Remove one'),
      '#submit' =&gt; array('ajax_example_add_more_remove_one'),
      '#ajax' =&gt; array(
        'callback' =&gt; 'ajax_example_add_more_callback',
        'wrapper' =&gt; 'names-fieldset-wrapper',
      ),
    );
  }
  $form['submit'] = array(
    '#type' =&gt; 'submit',
    '#value' =&gt; t('Submit'),
  );
  if ($no_js_use) {
    // Remove the #ajax from the above, so ajax.js won't be loaded.
    if (!empty($form['names_fieldset']['remove_name']['#ajax'])) {
      unset($form['names_fieldset']['remove_name']['#ajax']);
    }
    unset($form['names_fieldset']['add_name']['#ajax']);
  }

  return $form;
}

/**
 * Callback for both ajax-enabled buttons.
 *
 * Selects and returns the fieldset with the names in it.
 */
function ajax_example_add_more_callback($form, $form_state) {
  return $form['names_fieldset'];
}

/**
 * Submit handler for the "add-one-more" button.
 *
 * Increments the max counter and causes a rebuild.
 */
function ajax_example_add_more_add_one($form, &amp;$form_state) {
  $form_state['num_names']++;
  $form_state['rebuild'] = TRUE;
}

/**
 * Submit handler for the "remove one" button.
 *
 * Decrements the max counter and causes a form rebuild.
 */
function ajax_example_add_more_remove_one($form, &amp;$form_state) {
  if ($form_state['num_names'] &gt; 1) {
    $form_state['num_names']--;
  }
  $form_state['rebuild'] = TRUE;
}


The above code is taken from the example module with little modification. In this code i have added one more text field called surname with name textfield.

Now when i access form it gives me the form fields as shown in the image given below.
Before clicking on Add more button:


Now when i click on the Add more button two fields are added as shown in the below image.



Here you can notice that there are two continuous Name and then two continuous Surname fields.

But actually i want to show these fields like in group. So the first group of name and surname and then second group of name and surname field.



How can i achieve as per the above snapshot ?
The answer is - you really shouldn't try. The logic behind your form now shows one person with two Names and two SurNames. What you want, I bet, is two persons with one Name and one SurName each. So just group Name and SurName in fieldset and you're good.

If you insist on not doing it, read further. You use $form['names_fieldset']['name'][$i] and $form['names_fieldset']['surname'][$i]. It means $form['names_fieldset']['name'] is created before $form['names_fieldset']['surname'], so everything you put in it will be together and before surname. Just swap indexes and make them look like this: $form['names_fieldset'][$i]['name'], $form['names_fieldset'][$i]['surname'] and you'll be good. Of course $form['names_fieldset'][$i] can be a fieldset by itself, so we are back to first paragraph.