An illegal choice has been detected. Please contact the site administrator in drop down list when i am selecting values from dropdown list the corresponding values showing first time. but when i am again selecting the another value its not showing value instead of the error msg displaying An illegal choice has been detected. Please contact the site administrator. can anyone suggest me where i did wrong in the following code 

&lt;?php 

function form_test_menu() 
{
  $items['mark_entry'] = array(
    'title' =&gt; 'Mark Entry',
    'page callback' =&gt; 'drupal_get_form',
    'page arguments' =&gt; array('form_test_form'),
    'access callback' =&gt; TRUE,
    );
  return $items;
}


function form_test_form($form,&amp;$form_submit) 
{

$dept = $_POST['dept'];
$batch = $_POST['batch'];
$month = $_POST['Month'];
$year = $_POST['Year'];
$semester = $_POST['semester'];

$query = db_select('student_master');
$query-&gt;fields('student_master', array('reg_no','name','dob','dept_code','degree','batch_year'));
$query-&gt;condition('dept_code',$dept,'=') AND $query-&gt;condition('batch_year',$batch,'=');
$results = $query-&gt;execute();
 $options = array();

foreach($results as $student_result)
{ 
$options[$student_result-&gt;reg_no]=t($student_result-&gt;reg_no);
} 

  $form['roll']= array(
  '#type' =&gt; 'select',
  '#title' =&gt; t('Registration Number'),
  '#options' =&gt;$options,
  '#required' =&gt; TRUE,
  '#ajax' =&gt; array('callback' =&gt;       '_ajaxfunction','wrapper' =&gt; 'subjectsdiv'),);

 $form['month'] = array('#title' =&gt; t('month'),
    '#type' =&gt; 'hidden',
    '#value' =&gt; $month,
'#required' =&gt; TRUE,);

 $form['year'] = array('#title' =&gt; t('year'),
    '#type' =&gt; 'hidden',
    '#value' =&gt; $year,
'#required' =&gt; TRUE,);

 $form['semester'] = array('#title' =&gt; t('semester'),
    '#type' =&gt; 'hidden',
    '#value' =&gt; $semester,
'#required' =&gt; TRUE,);

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

$form['submit'] = array('#type' =&gt; 'submit','#value' =&gt; 'Submit');

return $form;
}


function _ajaxfunction($form, $form_state) 

{ 


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

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

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

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

$query1 = db_select('semester_appear');
$query1-&gt;fields('semester_appear',array('subject_code'));
$query1-&gt;condition('reg_no',$key,'=') AND $query1-&gt;condition('semester_appear',$semester,'=')AND $query1-&gt;condition('month',$month,'=') AND $query1-&gt;condition('year',$year,'=');
$query1-&gt;orderBy('sub_serial_no','ASC');
$results = $query1-&gt;execute();

while ($record_res = $results-&gt;fetchAssoc())
{

$subject_name = $record_res['subject_code'];

$query = db_select('subject');
$query-&gt;fields('subject', array('subject_name','subject_code'));
$query-&gt;condition('subject_code',$subject_name,'=') ;
$subject_results = $query-&gt;execute();

foreach($subject_results as $sub_result)
{ 
$options[$sub_result-&gt;subject_code]=t($sub_result-&gt;subject_name);
}

  $form['subjects']['name']= array(
    '#type' =&gt; 'select',
    '#title' =&gt; t('Please Select Subject'),
    '#options' =&gt; $options,
'#required' =&gt; TRUE,);

$form['subjects']['inter'] = array(
  '#type' =&gt; 'textfield', 
  '#title' =&gt; t('Internal Marks'), 
  '#size' =&gt; 60, 
  '#maxlength' =&gt; 128, 
  '#required' =&gt; TRUE,);


$form['subjects']['exter'] = array(
  '#type' =&gt; 'textfield', 
  '#title' =&gt; t('External Marks'), 
  '#size' =&gt; 60, 
  '#maxlength' =&gt; 128, 
  '#required' =&gt; TRUE,);

}

return $form['subjects']; 
}
check for details in watchdog under form to get an indication of which form element is causing the grief - in your case it will be the 'roll' or subject name field (possibly both!)

if you are updating the option array and removing the currently set option, then you have 2 choices:

in your form builder, either 

unset($form_state['input']['roll']);


or

$options[$form_state['values']['roll']] 
 = "the option i'm trying to get rid of";  


the first option just discards the earlier selection, the latter option keeps it available (probably the first option is generally preferable)

additional: I've discovered I can also cause this error by messing my ajax call back so that the options are updated ok, but the form element in the browser isn't updated. In this case you will notice your options are not updating. You then need to double check your callbacks and div id's etc.

hth