Values disappear in $form_state['values'] I have some tableselect in a form which I can click a button and update from a database. When I update it, I need to be able to access those values in my submit function. But those values aren't available in $form_state['values'] if I have updated the tableselect via AJAX. How can I make those available, or are they not available before the form is submitted?

Here is the AJAX function:

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

  $tableselect_header = array(
    'id' =&gt; t('ID'),
    'row_id' =&gt; t('Room number'),
    'start-end' =&gt; t('Start date - end date'),
    'sum' =&gt; t('Sum'),
    'type' =&gt; t('Type'),
  );

  foreach ($form['data_tableselect'] as $data =&gt; $val) {
    if (is_int($data)) {
      unset($form['data_tableselect'][$data]);
    }
  }

  // This function just gets the data from the database and returns it as an array
  $lines = module_data_from_database($form, $form_state, true);
  $form['data_tableselect'] = array(
    '#type' =&gt; 'tableselect',
    '#tree' =&gt; 'true', 
    '#attributes' =&gt; array(
      'id' =&gt; 'data_tableselect'
    ),
    '#header' =&gt; $tableselect_header,
    '#options' =&gt; $lines,
    '#multiple' =&gt; true,
    '#empty' =&gt; t('No data to show.'),
    '#parents' =&gt; array('data_tableselect'),
    '#default_value' =&gt; '',
    '#weight' =&gt; 14
  );

  $form['data_tableselect'] = form_process_tableselect($form['data_tableselect']);

  $commands = array();
  $commands[] = ajax_command_replace("#data_tableselect", render($form[['data_tableselect']));

  return array('#type' =&gt; 'ajax', '#commands' =&gt; $commands);


}

And here is the relevant part of the hook_form_alter function where I defined the button that triggers the above function. The code that defines the tableselect there is much the same as that above:

$form['data_get'] = array(
  '#type' =&gt; 'submit',
  '#value' =&gt; t('Import data'),
  '#attributes' =&gt; array(
    'class' =&gt; array('col-sm-3', 'col-xs-4')
  ),
  '#parents' =&gt; array('data_get'),
  '#limit_validation_errors' =&gt; array(),
  '#ajax' =&gt; array(
    "callback" =&gt; 'module_data_table',
    "wrapper" =&gt; 'data_tableselect',
    'progress' =&gt; array(
      'type' =&gt; 'throbber',
      'message' =&gt; t('Importing...')
    ),
    "event" =&gt; 'click'
  )
);
You have to move the code for changing form elements from ajax callback to form build (which includes hook_form_alter()). The form build is executed for every ajax request before the callback function is called. In the callback you are not allowed to change the form array. Put only code there to return form elements.

More information: Ajax Forms in Drupal 7

Quote:


  Changes to the form must only be made in the form builder function
  (ajax_example_autocheckboxes() in the example here), or validation
  will fail. The callback function must not alter the form or any other
  state.