Drupal 7 dynamic dropdown list options An illegal choice has been detected I have created a node type of Advertisement which holds all the information for adding new advertations to the website, including categories.

there are 3 levels of categories and i need to make a seperate box for them. The content type which i created through the GUI of drupal already has a term reference select box for category, sub category, and level 3 category.. 
So i made a module and implemented hook_form_alter and i added the #ajax attributefunction 

&lt;?php
adverts_form_alter(&amp;$form, &amp;$form_state, $form_id) {
  if($form_id == 'advertisement_node_form') {
      $topLevelTermArray = array('_none' =&gt; ' - Select a value -');
      $topLevelTerms = array(5275, 5245, 5299, 5341, 5702, 5681, 5826, 5555, 6039, 5592, 5776, 5994, 6027, 5451, 5852, 6016, 5496, 5475, 6049);
      foreach($topLevelTerms AS $value) {
        $term = taxonomy_term_load($value);
        $topLevelTermArray[$term-&gt;tid] = $term-&gt;name;
      }
      $form['field_specific_category']['und']['#prefix'] = '&lt;div id="field_specific_category_replace"&gt;';
      $form['field_specific_category']['und']['#suffix'] = '&lt;/div&gt;';
      $form['field_sub_category']['und']['#ajax'] = array(
        'callback' =&gt; 'ajax_sub_category_callback',
        'wrapper' =&gt; 'field_specific_category_replace',
      );
      $form['field_sub_category']['und']['#prefix'] = '&lt;div id="field_sub_category_replace"&gt;';
      $form['field_sub_category']['und']['#suffix'] = '&lt;/div&gt;';
      $form['field_sub_category']['und']['#options'] = array('_none' =&gt; t('- Please select a category -'));
      $form['field_specific_category']['und']['#options'] = array('_none' =&gt; t('- Please select a sub category -'));
      $form['field_category']['und']['#options'] = $topLevelTermArray;
      $form['field_category']['und']['#ajax'] = array(
        'callback' =&gt; 'ajax_category_callback',
        'wrapper' =&gt; 'field_sub_category_replace',
      );
  }
}
?&gt;


And 2 callback functions

function ajax_category_callback($form, $form_state) {
  $subCats = array();
  $tree = taxonomy_get_tree(5, $form_state['values']['field_category']['und'][0]['tid']);
  foreach($tree AS $term) {
    $subCats[$term-&gt;tid] = $term-&gt;name;
  }
  $element = $form['field_sub_category']['und'];
  $mainCatTerm = taxonomy_term_load($form_state['values']['field_category']['und'][0]['tid']);
  $element['#title'] = t('Sub categories for @cat.', array('@cat' =&gt; $mainCatTerm-&gt;name));
  $element['#options'] = $subCats;

  return $element;
}

function ajax_sub_category_callback($form, $form_state) {
  $subCats = array();
  $tree = taxonomy_get_tree(5, $form_state['values']['field_sub_category']['und'][0]['tid']);
  //var_dump($form_state['values']['field_sub_category']['und'][0]['tid']);
  foreach($tree AS $term) {
    $subCats[$term-&gt;tid] = $term-&gt;name;
  }
  $element = $form['field_specific_category']['und'];
  if(empty($tree)) {
    $element['#options'] = array('_none' =&gt; '- No specific category -');
  } else {
    $mainCatTerm = taxonomy_term_load($form_state['values']['field_sub_category']['und'][0]['tid']);
    $element['#title'] = t('Sub categories for @cat.', array('@cat' =&gt; $mainCatTerm-&gt;name));
    $element['#options'] = $subCats;
  }
  return $element;
}


But when i go to node/advertisement/edit and i select a category and sub category is show's:

An illegal choice has been detected. Please contact the site administrator.


How can i get that error too dissapear?
The value you are submitting was not present during the page or form loading time. 

If you populate some values using ajax which was not present in initial page loads and try to submit the form, then Drupal will not allow you to submit the form. It will always shows "An illegal choice has been detected" message.