An illegal choice has been detected. Please contact the site administrator.in dynamic select list dependent dropdown using hook_form_alter and custom form elements below

$form['get-quotes-shipping']['country'] = array(
      '#type' =&gt; 'select',
      '#options' =&gt; array('_none' =&gt; '- None -') + country_get_list(),
      '#multiple' =&gt; FALSE,
      '#weight' =&gt; 3,
      '#title'=&gt;t('Country'),
      '#default_value' =&gt; (isset($_SESSION['selected_country'])) ? $_SESSION['selected_country'] : '_none',
      '#ajax' =&gt; array(
          'event' =&gt; 'change',
            'wrapper' =&gt; 'squadron-wrapper',
            'callback' =&gt; '_get_state_field',
            'method' =&gt; 'replace',
       ),
    );






    $form['get-quotes-shipping2']['actions']['get_quotes'] = array(
      '#type' =&gt;'submit',
      '#value' =&gt; 'Get Quotes',
       '#submit' =&gt; array('custom_changes_get_quotes_submit'),
      '#validate' =&gt; array('custom_changes_get_quotes_validate'),
      '#weight' =&gt; 1,
    );


  $squadron_options = array();
   if (isset($_SESSION['selected_country'])) {
       module_load_include('inc', 'location');
       $states = location_get_provinces($_SESSION['selected_country']);
       $squadron_options =  $states;
   }
   $form['get-quotes-shipping0']['states']['#type'] = 'select'; 
     $form['get-quotes-shipping0']['states']['#title'] = t('State/Province');


   $form['get-quotes-shipping0']['states']['#prefix'] = '&lt;div id="squadron-wrapper"&gt;';
   $form['get-quotes-shipping0']['states']['#suffix'] = '&lt;/div&gt;';
    $form['get-quotes-shipping0']['states']['#options'] = array('_none' =&gt; '- None -') + $squadron_options;
    if (isset($squadron_options[0])) {
      $form['get-quotes-shipping0']['states']['#default_value'] =  $squadron_options[0];
    }




Callback function as given below. 

function _get_state_field(&amp;$form,&amp;$form_state) {
  module_load_include('inc', 'location');
  if ($form_state['values']['country'] == '_none') {
    unset($form['get-quotes-shipping0']['states']['#options']);
    $form['get-quotes-shipping0']['states']['#default_value'] = '_none';
     return $form['get-quotes-shipping0']['states'];

  }
  else {
      $states = location_get_provinces($form_state['values']['country']);
 $form['get-quotes-shipping0']['states']['#options'] = $states;
     return $form['get-quotes-shipping0']['states'];

  }

}


When I select Country and then state at first it gives proper values
but when I select country again it gives 
An illegal choice has been detected

What is the issue with the above code?
can anyone point out?
i recently made a 3 level dependent dropdown and had the same issue with some of the problems i found on the way. First you need to re-organize your code a little bit more. have in mind that the callback function should normaly look something like this:

//callback
    function _get_state_field(&amp;$form,&amp;$form_state) {

     return $form['get-quotes-shipping0']['states'];
}


Any code inside the callback has the potential to intervene with the proccess, with some exceptions.

Without taking a longer look to your code i think it should look somethink like this:

&lt;?php

 module_load_include('inc', 'location');
  if ($form_state['values']['country'] == '_none') {
    unset($form['get-quotes-shipping0']['states']['#options']);
    $form['get-quotes-shipping0']['states']['#default_value'] = '_none';
     return $form['get-quotes-shipping0']['states'];

  }
  else {
      $states = location_get_provinces($form_state['values']['country']);
 $form['get-quotes-shipping0']['states']['#options'] = $states;
  }

$form['get-quotes-shipping']['country'] = array(
      '#type' =&gt; 'select',
      '#options' =&gt; array('_none' =&gt; '- None -') + country_get_list(),
      '#multiple' =&gt; FALSE,
      '#weight' =&gt; 3,
      '#title'=&gt;t('Country'),
      '#default_value' =&gt; (isset($_SESSION['selected_country'])) ? $_SESSION['selected_country'] : '_none',
      '#ajax' =&gt; array(
          'event' =&gt; 'change',
            'wrapper' =&gt; 'squadron-wrapper',
            'callback' =&gt; '_get_state_field',
            'method' =&gt; 'replace',
       ),
    );


    $form['get-quotes-shipping2']['actions']['get_quotes'] = array(
      '#type' =&gt;'submit',
      '#value' =&gt; 'Get Quotes',
       '#submit' =&gt; array('custom_changes_get_quotes_submit'),
      '#validate' =&gt; array('custom_changes_get_quotes_validate'),
      '#weight' =&gt; 1,
    );



   $form['get-quotes-shipping0']['states']['#type'] = 'select'; 
     $form['get-quotes-shipping0']['states']['#title'] = t('State/Province');


   $form['get-quotes-shipping0']['states']['#prefix'] = '&lt;div id="squadron-wrapper"&gt;';
   $form['get-quotes-shipping0']['states']['#suffix'] = '&lt;/div&gt;';
    $form['get-quotes-shipping0']['states']['#options'] = array('_none' =&gt; '- None -') + $squadron_options;
    if (isset($squadron_options[0])) {
      $form['get-quotes-shipping0']['states']['#default_value'] =  $squadron_options[0];
    }


    //callback
    function _get_state_field(&amp;$form,&amp;$form_state) {

     return $form['get-quotes-shipping0']['states'];
}
?&gt;


This is the code i wrote, try to follow the same structure it's working for me perfectly. 

Tell us how it went, have a good coding!