Form API ajax add group of users and numbers Im quite a newbe to Drupal, but have made some php.

My problem is that by the help of ajax example pages i've made ajax function to show two fields when checkbox is 1.

When i press the add button i get two more fields, but they are not sortet the way i want to.

The fields are Number and User.

The initial layout when checkbox is checked is:

Number

User

When i press the Add button i get:

Number
Number

User
User

instead of:

Number
User

Number
User

This is my full custom module code:

/*
 * Implements hook_menu().
 */
function test_menu() {
  $items['test'] = array(
    'title' =&gt; 'Test functions',
    'page callback' =&gt; 'drupal_get_form',
    'page arguments' =&gt; array('test_form'),
    'description' =&gt; 'Test functions.',
    'access callback' =&gt; TRUE,
  );
  return $items;
}


function test_form($form, &amp;$form_state) {

  $form['#tree'] = TRUE;

  $form['terms3'] = array(
    '#type' =&gt; 'checkbox',
    '#title' =&gt; t('add users.'),
    '#ajax' =&gt; array(
      'callback' =&gt; 'test_form_callback',
      'wrapper' =&gt; 'options',
      'effect' =&gt; 'fade',
    ),
  );

  $form['options'] = array(
    '#title' =&gt; t("Generated text fields users and usernumber"),
    '#prefix' =&gt; '&lt;div id="options"&gt;',
    '#suffix' =&gt; '&lt;/div&gt;',
    '#type' =&gt; 'container',
    '#description' =&gt; t('This is where we put automatically generated textfields'),
  );

  // Since checkboxes return TRUE or FALSE, we have to check that
  // $form_state has been filled as well as what it contains.
  if (!empty($form_state['values']['terms3']) &amp;&amp; $form_state['values']['terms3']) {
      if (empty($form_state['num_names'])) {
        $form_state['num_names'] = 1;
        }
        for ($i = 0; $i &lt; $form_state['num_names']; $i++) {
            $form['options']['number'][$i] = array(
                '#type' =&gt; 'textfield',
                '#title' =&gt; t('number:'),
            );
            $form['options']['Name'][$i] = array(
                '#type' =&gt; 'textfield',
                '#title' =&gt; t('Name of user:'),
            );
  }
          $form['options']['add_user'] = array(
            '#type' =&gt; 'submit',
            '#value' =&gt; t('Add'),
            '#submit' =&gt; array('test_form_add_more_add_one'),
            // See the examples in ajax_example.module for more details on the
            // properties of #ajax.
            '#ajax' =&gt; array(
              'callback' =&gt; 'test_form_callback',
              'wrapper' =&gt; 'options',
            ),
          );
          if ($form_state['num_names'] &gt; 1) {
            $form['options']['remove_user'] = array(
              '#type' =&gt; 'submit',
              '#value' =&gt; t('Remove'),
              '#submit' =&gt; array('test_form_add_more_remove_one'),
              '#ajax' =&gt; array(
                'callback' =&gt; 'test_form_callback',
                'wrapper' =&gt; 'options',
              ),
            );
  }
  }

  $form['submit'] = array(
    '#type' =&gt; 'submit',
    '#value' =&gt; t('Click Me'),
  );

  return $form;
}

/**
 * Callback for autotextfields.
 *
 * Selects the piece of the form we want to use as replacement text and returns
 * it as a form (renderable array).
 *
 * @return array
 *   Renderable array (the textfields element)
 */
function test_form_callback($form, $form_state) {
  return $form['options'];
}

/**
 * Submit handler for the "add-one-more" button.
 *
 * Increments the max counter and causes a rebuild.
 */
function test_form_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 test_form_add_more_remove_one($form, &amp;$form_state) {
  if ($form_state['num_names'] &gt; 1) {
    $form_state['num_names']--;
  }
  $form_state['rebuild'] = TRUE;
}


Hope someone can help.
You just have to move the $i value.

Instead of

for ($i = 0; $i &lt; $form_state['num_names']; $i++) {
            $form['options']['number'][$i] = array(
                '#type' =&gt; 'textfield',
                '#title' =&gt; t('number:'),
            );
            $form['options']['Name'][$i] = array(
                '#type' =&gt; 'textfield',
                '#title' =&gt; t('Name of user:'),
            );
  }


Use

for ($i = 0; $i &lt; $form_state['num_names']; $i++) {
            $form['options'][$i]['number'] = array(
                '#type' =&gt; 'textfield',
                '#title' =&gt; t('number:'),
            );
            $form['options'][$i]['Name'] = array(
                '#type' =&gt; 'textfield',
                '#title' =&gt; t('Name of user:'),
            );
  }