How to get selected value in option id value in drupal form api Hi hello please help how to get dynamically selected option id value (i.e #options=>0=>'bala',1=>'kumar')

for example (1001->india,1002->usa) like that stored in select option value (india, usa like that) , but i want to fetch the id value(1001, 1002) of selected value of id value from option? that value to be store in $key variable, please help me 

$query_state=db_select('state', 's');
$query_state-&gt;fields('s',array('State_code','State_name'));
$query_state-&gt;orderBy('State_name','ASC');
$state_results = $query_state-&gt;execute();
$options_state = array();

foreach($state_results as $state_record) {
  $options_state[$state_record-&gt;State_name]=t($state_record-&gt;State_code);
}

$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'
  ),
);

// here values are popup in select option component but corresponding value of state in city select option not visible 


 $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) {
  $options_city[$city_record-&gt;District_Code]=t($city_record-&gt;District_Name);
}

$form['city']= array(
  '#type' =&gt; 'select',
  '#title' =&gt; t('District'),
  '#options' =&gt; $options_city,
  '#prefix' =&gt; '&lt;div id="divaroundseconddropdown"&gt;',
  '#suffix' =&gt; '&lt;/div&gt;',
  '#required' =&gt; TRUE,
  '#states'=&gt;array(
    'visible'=&gt;array(
      ':input[name="state1"]' =&gt; array(
          'value' =&gt; $options_state,
      ),
    ),
  ),
);


function _ajaxfunction($form, $form_state) { 
  return $form['city'];
}
Try the following code:

function YOUR_FUNCTION_THAT_DISPLAYS_FORM(&amp;$form, &amp;$form_state) {
  $query_state=db_select('state', 's');
  $query_state-&gt;fields('s',array('State_code','State_name'));
  $query_state-&gt;orderBy('State_name','ASC');
  $state_results = $query_state-&gt;execute();

  $options_state = array();

  // Using fetchAll() you can iterate through a result set.
  while ($state_record = $state_results-&gt;fetchAll()) {
    // You want state code as key, not state name as key.
    $options_state[$state_record-&gt;State_code]=t($state_record-&gt;State_name);
  }

  // State form element.
  $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'
      'method' =&gt; 'replace',
      'effect' =&gt; 'fade',
    ),
  );

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

  return $form;
}




// AJAX callback function.
function _ajaxfunction(&amp;$form, &amp;$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();


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

  // Render city setting.
  $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'];
}


Hope this helps you!