title: WordPress - Get Database results with ajax
body: <p>Good morning!  I'm just trying out AJAX in a WordPress plugin for the first time, and I'm a bit stumped.  </p>
  
  <p>Basically, I have 2 divs next to each other.  Left one has a list of cell phone numbers, right one should contain the conversation with the cell number that the user clicks on.  (it will look a lot like the messaging app on an iPad). 
  I don't want to re-load the page to switch to a different conversation, so I thought this was a good plan.  Correct me if I'm wrong! </p>
  
  <p>So I've got this HTML: (abbreviated for simplicity)</p>
  
  <pre><code>&lt;div class="container"&gt;
    &lt;div class="list_cell_numbers"&gt;
      &lt;a href="" class="ajax-link" id="1238675309"&gt;123-867-5309&lt;/a&gt;
      &lt;a href="" class="ajax-link" id="9035768321"&gt;903-576-8321&lt;/a&gt;
    &lt;/div&gt;
  
    &lt;div class="show_conversation"&gt;
      &lt;!--Display Database Results Here --&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Here is my jQuery:</p>
  
  <pre><code>jQuery(document).ready( function($) {
    $(".ajax-link").click( function() {
      var data = {
        action: 'sms_load_conversation',
        cell_number: $(this).attr('id')
      };
  
    $.post('http://example.com/wp-admin/admin-ajax.php', data,
      function(response) {
        alert(response)
      });
  
    });
  
  });
  </code></pre>
  
  <p>Here is my Wordpress Function that handles the AJAX call:</p>
  
  <pre><code>add_action('wp_ajax_sms_load_conversation', 'sms_load_conversation');
  function sms_load_conversation() {
    global $wpdb;
    if( isset( $_POST["cell_number"] ) ) {
      $number = $_POST["cell_number"];
      $msgs = $wpdb-&gt;get_row("SELECT message, sent_date FROM sms_log WHERE cell_number = $number LIMIT 1");
  
      echo $msgs-&gt;message;
  
      wp_die();
    }
  }
  </code></pre>
  
  <p>So with this code I have gotten to the point where I can click on the link, and the proper result is returned - the message from the database.  Where I'm stuck is: 
  1- how to actually place this content into my "show_conversation" div on the page once it's returned, and
  2- how can I have the AJAX respond with multiple database results with multiple fields?  I feel like I need to have the sms_load_conversation function get the db results, format the results as HTML and return the entire HTML back to my "show_conversation" div, but I'm not entirely sure how I would go about returning that much information.  Is it just one HUGE string? </p>
  
  <p>Thank you so much in advance if anyone can offer some direction!!!</p>