Change $form_state in AJAX callback I am creating a form which uses the form API '#ajax' attribute, and I need to make changes to $form_state when the AJAX-enabled button is pressed. Here's what I've got:

Inside my form builder function:

...
$form['some_fieldset']['add_more'] = array(
      '#type' =&gt; 'button',
      '#value' =&gt; t('Add more rows'),
      '#ajax' =&gt; array(
        'wrapper' =&gt; 'some-fieldset',
        'callback' =&gt; 'my_module_settings_ajax',
        'method' =&gt; 'replace',
        'effect' =&gt; 'fade',
      ),
    );

...


And here's the callback function:

function my_module_settings_ajax($form, $form_state) { 
  // Make a change to $form_state
  if ($form_state['some_fieldset']['add_more']) {
    $form_state['values']['some_fieldset']['table'][] = array('first_field' =&gt; '',
                                                                'second_field' =&gt; '');
  }

  return $form['some_fieldset'];
}


The change I've made to $form_state seem to be lost once the callback has finished. Is there any way to make a change to $form_state in this function, or there any other function I should use for this instead?

EDIT

As it turns out, the callback is the wrong place to do this logic. Instead, at the top of the form builder function, I added this:

// Respond to AJAX-triggger:
// Add new row
if ($form_state['triggering_element']['#name'] == 'add_more') {
  $form_state['values']['some_fieldset']['table'][] = array('first_field' =&gt; '',
                                                                'second_field' =&gt; '');
}


Of course, I had to give the button the name:

$form['some_fieldset']['add_more'] = array(
  '#type' =&gt; 'button',
  '#name' =&gt; 'add_more', // &lt;-- Right here
  '#value' =&gt; t('Add more rows'),
  '#ajax' =&gt; array(
    'wrapper' =&gt; 'some-fieldset',
    'callback' =&gt; 'my_module_settings_ajax',
    'method' =&gt; 'replace',
    'effect' =&gt; 'fade',
  ),
);


And now it works!
You need to pass $form_state as reference; the AJAX function needs to be defined as in the following example:

function mymodule_settings_ajax($form, &amp;$form_state) { /* … */ }