get form_state values from ajax callback through hook_form_alter? I'm having a little problem with my drupal 7 module.
I'm trying to get the $form_state['values'] from $form['base']['st-volume-pricing']['price'] and $form['base']['st-volume-pricing']['till-amount'] in my ajax callback so that I can add them to a results table.

Is this the right way to do it or am I missing something?

function mymodule_form_alter($form, &amp;$form_state) { 
  $form['base']['st-volume-pricing']['price'] = array(
    '#type' =&gt; 'uc_price',
    '#title' =&gt; t('Price'),
    '#description' =&gt; t('Set the price for this volume\'s reduction'),
    '#size' =&gt; 10,
    '#prefix' =&gt; '&lt;table id="volume-pricing-standard-input"&gt;&lt;tr&gt;&lt;td&gt;',
    '#suffix' =&gt; '&lt;/td&gt;',
  );
  $form['base']['st-volume-pricing']['till-amount'] = array(        
    '#type' =&gt; 'textfield',
    '#title' =&gt; t('Till Amount'),
    '#description' =&gt; t('The amount till which the volume discount applies'),
    '#size' =&gt; 10,
    '#prefix' =&gt; '&lt;td&gt;',
    '#suffix' =&gt; '&lt;/td&gt;',
  );
  $form['base']['st-volume-pricing']['add'] = array(
    '#type' =&gt; 'submit',
    '#value' =&gt; t('Add'),
    '#submit' =&gt; array('mymodule_add_volume_standard'),
    '#ajax' =&gt; array(
      'callback' =&gt; 'mymodule_add_volume_standard_ajax_callback',
      'wrapper' =&gt; 'st-volume-results',
      'method' =&gt; 'append',
      'effect' =&gt; 'fade',
    ),
    '#prefix' =&gt; '&lt;td&gt;',
    '#suffix' =&gt; '&lt;/td&gt;&lt;/tr&gt;',
    '#title' =&gt; t('Click this button to add a standard volume price'),
  );

  //this is our results table
  $form['base']['st-volume-pricing']['st-volume-results'] = array(
    '#type' =&gt; 'markup',
    '#markup' =&gt; '&lt;table id="st-volume-results"&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;' . t('Price') . '&lt;/th&gt;&lt;th&gt;' . t('From Amount') . '&lt;/th&gt;&lt;th&gt;' . t('Till Amount') . '&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;/table&gt;',
  );
  variable_set('vol-st-price', NULL)
  variable_set('vol-st-from', 1)
  variable_set('vol-st-till', NULL)

  //this is our row that will be inserted
  $form['base']['st-volume-pricing']['st-volume-results']['row'] = array(
    '#type' =&gt; 'markup',
    '#markup' =&gt; '&lt;tr&gt;&lt;td&gt;' . variable_get('vol-st-price') . '&lt;/td&gt;&lt;td&gt;' . variable_get('vol-st-from') . '&lt;/td&gt;&lt;td&gt;' . variable_get('vol-st-till') . '&lt;/td&gt;&lt;/tr&gt;',
  );
}


When I'm using it inside here:

function mymodule_add_volume_standard_ajax_callback($form, &amp;$form_state) {
  //check if there is a previous volume price and set the from field
  if (variable_get('vol-st-till') !== NULL) {
    variable_set('vol-st-from') = variable_get('vol-st-till') + 1;
  }; 
  //add or overwrite the value entered in price
  variable_set('vol-st-price') = $form_state['values']['base']['st-volume-pricing']['price'];
  //add or overwrite the value entered in till
  variable_set('vol-st-till') = $form_state['values']['base']['st-volume-pricing']['till-amount'];
  //create new row in results table
  return $form['base']['st-volume-pricing']['st-volume-results']['row'];
}


drupal_set_message("&lt;pre&gt;" . print_r($form_state['values']) . "&lt;/pre&gt;");
displays 1.

I suppose that $form_state['values'] should only work during the submission of a form.
How would one go about doing this when using a hook_form_alter() and ajax callbacks?

I also tried displaying drupal_set_message("&lt;pre&gt;" . print_r($form_state['values']) . "&lt;/pre&gt;"); in my function mymodule_add_volume_standard. Which also displayed 1.
Neither does it work when trying it in the hook_form_alter() which is default behaviour I suppose?

I'd really appreciate it if someone could clarify some things for me.
Some basics:

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



hook_form_alter is a generic alter function that receives a third argument $form_id to distinguish the possible forms. Right now you would alter every form in your site.
The correct function signature is:

function mymodule_form_alter(&amp;$form, $form_state, $form_id) {


You can see, that the $form itself is passed as a reference and can be changed. There is no way of changing $form_state directly during the form building process, which is when hook_form_alter is invoked.


Concerning $form_state['values'] in validation and submit handlers: All elements defined in the $form array are by default inserted into a flat structure in $form_state['values']. This means that with your setup, you would access the value entered in $form['base']['st-volume-pricing']['till-amount'] as $form_state['values']['till-amount']. You can change that behavior by defining #treefor the form element's parent, see for example: https://drupal.org/node/48643

Edit: The general approach for ajax forms is that in the submit you store values inside $form_state, e.g. in $form_state['storage'], set $form_state['rebuild'] which assures that the form builder and hence your alter function is called again, and then test for the presence of stored values inside your form alter function. Depending on the submitted values you would then implement the logic to achieve what you want.