Ajax HTTP Error Occured in Theming Tables I have two elements in my form: a select element and a div with an id of #myid_table_wrapper.

function myid_print_user_page_form()
{
    $form = array();

    $form['options'] = array(
        '#type' =&gt; 'select',        
        '#options' =&gt; array(
            0 =&gt; t('All'),
            1 =&gt; t("Printed ID's"),
            2 =&gt; t("Unprinted ID's")
        ),       
        '#ajax' =&gt; array(
            'callback' =&gt; 'myid_print_all_submitted_id_callback',
            'wrapper' =&gt; 'myid_table_wrapper',
        ),                             
    );
    $form['myid_table_wrapper'] = array(
        '#type' =&gt; 'item',                
        '#markup' =&gt; '&lt;div id= "myid_table_wrapper"&gt;&lt;/div&gt;',                              
    );
    return $form;
}


Everytime the select element fires an onchange event it calls a callback function named myid_print_all_submitted_id_callback.

function myid_print_all_submitted_id_callback($form, $form_state)
{    
    // Build the sortable table header.
    $header = array(
        'id_number' =&gt; array('data' =&gt; t('ID Number'), 'field' =&gt; 'id_number','sort' =&gt; 'asc'),     
        'student' =&gt; array('data' =&gt; t('Student'), 'field' =&gt; 'student', 'sort' =&gt; 'asc')
   );

   //Get the node data. 
   $query = db_select('student', 't1',array('target' =&gt; 'import'));
   $result = $query
       -&gt;fields('t1', array('studid','lastname', 'firstname', 'middlename', 'extname'))   
       -&gt;condition('t1.id_status',2,'=')
       -&gt;distinct()
       -&gt;execute();
   //Build the rows.
   $rows = array();  
   foreach($result as $record){
       $rows[$record-&gt;studid] = array(
           'id_number' =&gt; $record-&gt;studid,
           'student' =&gt; $record-&gt;firstname . ' ' . $record-&gt;middlename . ' ' . $record-&gt;lastname . ' ' . $record-&gt;extname,    
       );
   }
   return theme('table', array('header' =&gt; $header, 'rows'=&gt; $rows));
}


The code results to an error:



Whats wrong with my code? How will I debug this?
The correct way to do this is to populate the id table inside the form generation function (checking $form_state['values']). Then the callback should only return the form element.
This is an example of how I would do it:

function myid_print_user_page_form($form, $form_state)
{
    $form = array();

    $form['options'] = array(
        '#type' =&gt; 'select',        
        '#options' =&gt; array(
            0 =&gt; t('All'),
            1 =&gt; t("Printed ID's"),
            2 =&gt; t("Unprinted ID's")
        ),       
        '#ajax' =&gt; array(
            'callback' =&gt; 'myid_print_all_submitted_id_callback',
            'wrapper' =&gt; 'myid_table_wrapper_div',
        ),                             
    );
    $table_html = '';

    // Check if option has been selected.
    if(!empty($form_state['values']['options'])){
      // Build the sortable table header.
      $header = array(
          'id_number' =&gt; array('data' =&gt; t('ID Number'), 'field' =&gt;   'id_number','sort' =&gt; 'asc'),     
          'student' =&gt; array('data' =&gt; t('Student'), 'field' =&gt; 'student', 'sort' =&gt; 'asc')
       );
       //Get the node data. 
       $query = db_select('student', 't1',array('target' =&gt; 'import'));
       $result = $query
           -&gt;fields('t1', array('studid','lastname', 'firstname', 'middlename', 'extname'))   
           -&gt;condition('t1.id_status',2,'=')
           -&gt;distinct()
           -&gt;execute();
       //Build the rows.
       $rows = array();  
       foreach($result as $record){
           $rows[$record-&gt;studid] = array(
               'id_number' =&gt; $record-&gt;studid,
               'student' =&gt; $record-&gt;firstname . ' ' . $record-&gt;middlename . ' ' . $record-&gt;lastname . ' ' . $record-&gt;extname,    
           );
       }
       $table_html = theme('table', array('header' =&gt; $header, 'rows'=&gt; $rows));


    }

    $form['myid_table_wrapper'] = array(
        '#type' =&gt; 'item',                
        '#markup' =&gt; $table_html,
        '#prefix' =&gt; '&lt;div id="myid_table_wrapper_div"&gt;',
        '#suffix' =&gt; '&lt;/div&gt;',                              
    );
    return $form;
}

function myid_print_all_submitted_id_callback($form, $form_state)
{
    return $form['myid_table_wrapper'];
}


This is untested, but you get the idea.