How to store Ajax Return value into variable after clicking the Submit button I have created form with fields of State and City. After selecting the state appropriate city records will be loaded into city field till now working fine but after clicking the submit button I am not able to store city value into variable called $data1. Please guide me how to get it city value  

function form_test_form($form,&amp;$form_submit) 
{
$form['state1']= array(
  '#type' =&gt; 'select',
  '#title' =&gt; t('State'),
  '#options' =&gt;$options_state,
  '#required' =&gt; TRUE,
  '#ajax' =&gt; array(
    'callback' =&gt; '_ajaxfunction',
    'wrapper' =&gt; 'divaroundseconddropdown'
  ),
);

$form['city_element_wrapper'] = array(
    '#prefix' =&gt; '&lt;div id="divaroundseconddropdown"&gt;',
    '#suffix' =&gt; '&lt;/div&gt;',
  );

}


Ajax Function

 function _ajaxfunction($form, $form_state) 

    { 

$key = !empty($form_state['values']['state1']) ? $form_state['values']['state1'] : 1;

  $query_city=db_select('city', 'c');
  $query_city-&gt;fields('c',array('District_Code','District_Name','State_Code'));
  $query_city-&gt;condition('State_Code', $key, '=');

  $query_city-&gt;orderBy('District_Name','ASC');
  $city_results = $query_city-&gt;execute();
  $options_city = array();


  foreach ($city_results as $city_record)
{
    // This is correct.
    $options_city[$city_record-&gt;District_Code]=t($city_record-&gt;District_Name);
}

    $form['city_element_wrapper']['city']= array(
        '#type' =&gt; 'select',
        '#title' =&gt; t('District'),
        '#options' =&gt; $options_city,
        '#required' =&gt; TRUE,);

    return $form['city_element_wrapper']; 
    }


Now I am trying to store the value of what I have selected in City into $data1 variable but I am not able to store it, Please guide me how to store 

function form_test_custom_form_submit($form, $form_state){
    $data1 = $form_state['values']['city_element_wrapper']['city'];
}
You need to move the $form['city'] form input creation into your form_test_form function otherwise it's considered tampering with the form, so the values will get taken out.

function form_test_form($form,&amp;$form_submit) {
  $form['state1']= array(
    '#type' =&gt; 'select',
    '#title' =&gt; t('State'),
    '#options' =&gt;$options_state,
    '#required' =&gt; TRUE,
    '#ajax' =&gt; array(
      'callback' =&gt; '_ajaxfunction',
      'wrapper' =&gt; 'divaroundseconddropdown'
    ),
  );

  $form['city_element_wrapper'] = array(
    '#prefix' =&gt; '&lt;div id="divaroundseconddropdown"&gt;',
    '#suffix' =&gt; '&lt;/div&gt;',
  );

  // If there's a "State" value set, then we create the City dropdown
  if (isset($form_state['values']['state1']) &amp;&amp; !empty($form_state['values']['state1'])) {
    $form['city_element_wrapper']['city'] = array(
      '#type' =&gt; 'select',
      '#title' =&gt; t('District'),
      '#options' =&gt; $options_city,
      '#required' =&gt; TRUE,
    );
  }

  return $form;
}


Now the callback becomes a simple return of the relevant element:

function _ajaxfunction($form, $form_state) {
  return $form['city_element_wrapper']; 
}


Finally check the values of $form_state['values'] in your submit handler:

function form_test_custom_form_submit($form, $form_state){
  var_dump($form_state['values']);
  die();
}