title: Displaying data from database with AJAX, jquery and codeigniter
body: <p>The model seems to be working as well as the controller. The AJAX displays the results as "null" so I think it is because we need to send the data as json. Any ideas on how to get the data into the correct format and to display in the view</p>
  
  <p><strong>View</strong></p>
  
  <pre><code>&lt;button type='button' name='getdata' id='getdata'&gt;Get Data.&lt;/button&gt;
  
  &lt;div id='result_table' style="color:white;"&gt;
  hola amigo
  &lt;/div&gt;
  
  &lt;script type='text/javascript' language='javascript'&gt;
  $('#getdata').click(function(){
                  $.ajax({
                          url: 'http://localhost:8888/index.php/trial/getValues',
                           type:'POST',
                           dataType: 'json',
                            error: function(){
                            $('#result_table').append('&lt;p&gt;goodbye world&lt;/p&gt;');
                            },
  
                           success: function(results){
  
  
                         $('#result_table').append('&lt;p&gt;hello world&lt;/p&gt;' +results);
                         alert("Success!");
  
                            } // End of success function of ajax form
                            }); // End of ajax call
  
                  });
  &lt;/script&gt;
  </code></pre>
  
  <p><strong>Controller</strong></p>
  
  <pre><code>function getValues(){
      $this-&gt;load-&gt;model('get_db');
      $data['results'] = $this-&gt;get_db-&gt;getAll();
      $this-&gt;output-&gt;set_content_type('application/json');
      $this-&gt;output-&gt;set_output(json_encode($data));
      return $data;
  }
  </code></pre>
  
  <p><strong>Model</strong></p>
  
  <pre><code>class Get_db extends CI_Model{
      function getAll(){
          $query=$this-&gt;db-&gt;query("SELECT * FROM questions");
          return $query-&gt;result();
          //returns from this string in the db, converts it into an array
      }
  }
  </code></pre>
  
  <p>Okay so the AJAX returns the success alert, however instead of displaying the table from the database, this is what is displays in the div:</p>
  
  <p>Hello World</p>
  
  <p>null</p>
  
  <p>If I go directly to the url (<a href="http://loca.lhost:8888/index.php/trial/getValues" rel="nofollow">http://loca.lhost:8888/index.php/trial/getValues</a>)  this is the object that appears:</p>
  
  <pre><code>{
    "results": [
      {
        "qID": "1",
        "email": "hello",
        "qText": "hello",
        "playlistID": "",
        "timestamp": "0000-00-00 00:00:00"
      },
      {
        "qID": "2",
        "email": "",
        "qText": "",
        "playlistID": "",
        "timestamp": "0000-00-00 00:00:00"
      },
  
    }
  </code></pre>
  
  <p>How do I extract this info and display what I want to display?</p>