Form with AJAX callback to query database and display info in block['content'] I have a simple select form that I want to use as a "Sort By" handler. When user chooses a different option I want it to change the query and display the new results. E.G. sort by duration ASC or sort by duration DESC.

This is my form:

function mortgage_multi_view_sort_form($form, &amp;$form_state) {
     $form = array();
     $sortOptions = array(
        1 =&gt; t('Mortgage Duration: Shortest to Longest'),
        2 =&gt; t('Mortgage Duration: Longest to Shortest'),
     );

     $form['mortgage_sort'] = array(
        '#type' =&gt; 'select',
        '#title' =&gt; t('Sort By'),
        '#options' =&gt; $sortOptions,
        '#default_value' =&gt; 1,
        '#required' =&gt; TRUE,
        '#ajax' =&gt; array(
            'event' =&gt; 'change',
            'callback' =&gt; 'ajax_select_callback',
            //'wrapper' =&gt; 'replace_model_div',
            'method' =&gt; 'replace',
            //'effect' =&gt; 'fade',
        ),
     );

     return $form;
 }


When they change this to Longest to Shortest I want to asynchronously display the mortgages in reverse order. Is this best to do in the callback function? 

My callback function (currently):

 function ajax_select_callback($form, &amp;$form_state){
 switch($form['mortgage_sort']){
    case 1:
        $result = db_query('SELECT * FROM {mortgage_database} ORDERBY duration DESC');
        displayMortgages($result);
        break;
    case 2:
        $result = db_query('SELECT * FROM {mortgage_database} ORDERBY duration ASC');
        displayMortgages($result);
        break;
 } 


}

In my displayMortgages() function it just takes the entire query and displays it in a table. How do I get this information to block['content'] from my displayMortgages() now?
You should do all the logic in the form function. However with your particular issue, I would approach it slightly differently. I would simply return the table HTML from displayMortgages() back to the form and display the table as part of the form. But just simply as a #type=&gt; 'markup'. That way, your sort form and the results are one, and you don't need to worry about firing off another ajax command somewhere else.

So your form code would basically be:

function mortgage_multi_view_sort_form($form, &amp;$form_state) {
  $form = array();

  // Default sort direction
  $sort_dir = 'DESC';

  $sortOptions = array(
    'DESC' =&gt; t('Mortgage Duration: Shortest to Longest'),
    'ASC' =&gt; t('Mortgage Duration: Longest to Shortest'),
  );

  $form['mortgage_sort'] = array(
    '#type' =&gt; 'select',
    '#title' =&gt; t('Sort By'),
    '#options' =&gt; $sortOptions,
    '#default_value' =&gt; 'DESC',
    '#required' =&gt; TRUE,
    '#ajax' =&gt; array(
      'event' =&gt; 'change',
      'callback' =&gt; 'ajax_select_callback',
      'wrapper' =&gt; 'replace_model_div',
      'method' =&gt; 'replace',
      //'effect' =&gt; 'fade',
    ),
  );

  // Check if a selection on the form has been made, if so then update the direction
  if (isset($form_state['values']['mortgage_sort'])) {
    $sort_dir = $form_state['values']['mortgage_sort'];
  }

  $result = db_query('SELECT * FROM {mortgage_database} ORDERBY duration '.$sort_dir);

  $form['result'] = array(
    '#markup' =&gt; (count($result)) ? displayMortgages($result) : '',
    '#prefix' =&gt; '&lt;div id="replace_model_div"&gt;',
    '#suffix' =&gt; '&lt;/div&gt;',
  );

  return $form;
}


And you just need to return the part that's being replaced in your ajax callback:

function ajax_select_callback($form, &amp;$form_state) {
  // Just a return of result is sufficient since it's being replaced
  return $form['result'];
}