Update theme table using callback function in Ajax I have two elements in my form: a select and a table element. Everytime onchange event is triggered on select element, I want to update the values in my table. I want to do query and change the values of '#rows' in the table.

function myid_print_user_page_form($form, &amp;$form_state)
{
    $form = array();
    $form['myid_print']['option'] = array(
        '#type' =&gt; 'select',        
        '#options' =&gt; array(
            0 =&gt; t('All'),
            1 =&gt; t("Printed ID's"),
            2 =&gt; t("Unprinted ID's"),
        ),
        '#default_value' =&gt; 0,             
        '#ajax' =&gt; array(
            'callback' =&gt; 'myid_print_all_submitted_id_callback',
            'wrapper' =&gt; 'myid_table_wrapper',
        ),                             
    );

    // 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',1,'=')
        -&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,    
        );
    }

    //Build the table 
    $form['table'] = array(
        '#theme' =&gt; 'table',
        '#header' =&gt; $header,
        '#rows' =&gt; $rows,
        '#prefix' =&gt; '&lt;div id="myid_table_wrapper"&gt;',
        '#suffix' =&gt; '&lt;/div&gt;',
    );
    return $form;
}   


I'm stuck here, I don't know what to put in my callback function.

function myid_print_all_submitted_id_callback($form, $form_state)
{
    //Do query here and put the query results in the table.
}


Any help would be very much appreciated.
You can access the values of the form elements using the $form_state parameter. So when you have the value of the select element, you can perform the query as required, build the table using theme_table and return the result. The returned result can be replaced or appended to the ajax wrapper defined in your form.