Save values after add more ajax in form api - field name issue I've created a form using Form API and AJAX for an add more button, but I think I have problems assigning names to the new ajax fields in the loops, cause when I try to insert my data into my database I get an error:


  Undefined index


What am I doing wrong? Here's a section of my code:

&lt;?php
function main_form_form($form, &amp;$form_state) {

  $form['group_tabs'] = array(
      '#type'     =&gt; 'horizontal_tabs',
      '#attached' =&gt; array('library' =&gt; array(array('field_group', 'horizontal-tabs'))), // Converts fieldsets into tabs
      );

  $form['main_film_tab'] = array(
      '#type'  =&gt; 'fieldset',
      '#title' =&gt; t('Film General Info'),
      '#description' =&gt; t('Use this tab to enter basic info about the film.'),
      '#group' =&gt; 'group_tabs',
      );

  $form_state['storage']['main_film_tab']['add_title'] = isset($form_state['storage']['main_film_tab']['add_title']) ? $form_state['storage']['main_film_tab']['add_title'] : 0;

  $form['main_film_tab']['film_title']= array(
      '#type' =&gt; 'textfield',
      '#title' =&gt; t('Film Title'),
      '#size' =&gt; 60,
      '#maxlength' =&gt; 120,
      '#required' =&gt; TRUE,
      '#group' =&gt; 'main_film_tab',
      '#description' =&gt; t('Enter Film Title.'),
      '#attributes' =&gt; array(
        'title' =&gt; t("Title of Film"),
        )
      );

  $form['main_film_tab']['add_title'] = array(
      '#type' =&gt; 'container',
      '#tree' =&gt; TRUE,
      '#prefix' =&gt; '&lt;div id="add_title"&gt;',
      '#suffix' =&gt; '&lt;/div&gt;',
      );

  if ($form_state['storage']['main_film_tab']['add_title']) {
    for ($i = 1; $i &lt;= $form_state['storage']['main_film_tab']['add_title']; $i++) {
      if ($i &lt;= 2) {
        $form['main_film_tab']['add_title'][$i] = array(
            '#type' =&gt; 'fieldset',
            '#tree' =&gt; TRUE,
            );

        $form['main_film_tab']['add_title'][$i]['main_film_tab']['film_title_'.$i] = array(
            '#type' =&gt; 'textfield',
            '#title' =&gt; t('Film Title '.$i),
            '#size' =&gt; 60,
            '#maxlength' =&gt; 120,
            '#required' =&gt; FALSE,
            '#group' =&gt; 'main_film_tab',
            '#description' =&gt; t('Enter Film Title.'),
            '#attributes' =&gt; array(
              'title' =&gt; t("Title of Film"),
              )
            );
      }
      else {
        return FALSE;
      }
    }
  }

  $form['main_film_tab']['button_add_title'] = array(
      '#type' =&gt; 'button',
      '#value' =&gt; t('Add another title.'),
      '#href' =&gt; '',
      '#ajax' =&gt; array(
        'callback' =&gt; 'custom_registration_ajax_button_add_title',
        'wrapper' =&gt; 'add_title',
        ),
      );

  $form['main_film_tab']['button_add_title']['#executes_submit_callback'] = FALSE;
  $form['main_film_tab']['button_add_title']['#limit_validation_errors'] = array();

  $form['submit'] = array(
      '#type' =&gt; 'submit',
      '#value' =&gt; t('Save Film Info'),
      );

  $form_state['storage']['main_film_tab']['add_title']++;

  return $form;
}

function custom_registration_ajax_button_add_title($form, $form_state) {
  return $form['main_film_tab']['add_title'];
}

function main_form_form_submit($form, &amp;$form_state) {
  $film_id = db_insert('film_main')
    -&gt;fields(array(
          'film_title' =&gt; $form_state['values']['film_title'],
          'film_title_1' =&gt; $form_state['values']['film_title_1'],
          'film_title_2' =&gt; $form_state['values']['film_title_2'],
          ))
    -&gt;execute();
}
I was having problems with the fieldset, since it was set to #tree. The submit handler should look like this:

function main_form_form_submit($form, &amp;$form_state) {
  $film_id = db_insert('film_main')
  -&gt;fields(array(
     'film_title' =&gt; $form_state['values']['film_title'],
     'film_title_1' =&gt; $form_state['values']['add_title'][1]['main_film_tab']['film_title_1'],
     'film_title_2' =&gt; $form_state['values']['add_title'][2]['main_film_tab']['film_title_2'],
     ))
 -&gt;execute();