title: Codeigniter return values after ajax post
body: <p>Firstly sorry, I'm a really big beginner.</p>
  
  <p>I ran into a problem that I can't really solve, i am building a facebook-like wall. and after the post is sent, I would like the results to append to a div, but I'm totally lost.</p>
  
  <p>The problem is that it sends perfectly, but not shows the results</p>
  
  <p>Here is my code</p>
  
  <p>html </p>
  
  <pre><code>&lt;?php foreach($stream_posts as $sp): ?&gt;
      &lt;div id="sp_&lt;?php echo $sp-&gt;sid; ?&gt;" class="row stream-posts"&gt;
          &lt;div class="span1 stream-thumb"&gt;
              &lt;ul class="thumbnails"&gt;
                &lt;li&gt;
                  &lt;a href="#" class="thumbnail"&gt;
                    &lt;img src="http://placehold.it/60x60" alt=""&gt;
                  &lt;/a&gt;
                &lt;/li&gt;
              &lt;/ul&gt;
          &lt;/div&gt;  
          &lt;div class="span5 stream-content"&gt;
              &lt;a href="#" class="author"&gt;&lt;?php echo $sp-&gt;first_name ." ". $sp-&gt;last_name; ?&gt;&lt;/a&gt;
              &lt;p&gt;
                  &lt;?php echo nl2br($sp-&gt;text); ?&gt;
              &lt;/p&gt;
          &lt;/div&gt;
  
      &lt;/div&gt;
      &lt;?php endforeach; ?&gt;
  </code></pre>
  
  <p>jQuery </p>
  
  <pre><code>$('.stream-form').on('submit', function(){
          var streamData = $(this).serialize();
          $.ajax({
              url: "&lt;?php echo base_url(); ?&gt;stream/insert_stream",
              type: "POST",
              data: streamData,
              dataType: "json",
              success: function(stream) {
  
                  if(stream.error == 0)
                  {
                      $('.stream-text').val("");
                      $('.stream-posts').append().text(stream.text);
  
  
                  }   
              }
          });
          return false;
      });
  </code></pre>
  
  <p>In this jQuery code it appends all of it and breaks the div, i would like to show the new post what was submitted</p>
  
  <p>Model </p>
  
  <pre><code>    function add_stream()
      {
  
          $data = array(
              'added_by' =&gt; $this-&gt;session-&gt;userdata('user_id'),
              'text' =&gt; $this-&gt;input-&gt;post('stream_text')
          );
  
          $this-&gt;db-&gt;insert('pf_stream', $data);
  
  
      }
  
  function load_stream_post()
  {
      $this-&gt;db-&gt;select('*');
          $this-&gt;db-&gt;from('pf_stream');
          $this-&gt;db-&gt;join('pf_users', 'pf_users.id = pf_stream.added_by');
          $this-&gt;db-&gt;order_by('pf_stream.sid', 'DESC');
          $this-&gt;db-&gt;limit(1);
          $query = $this-&gt;db-&gt;get();
  
          if($query-&gt;num_rows() &gt; 0)
          {   
              foreach($query-&gt;result() as $row)
              {
                  $sdata[] = $row;
              }
          return $sdata;  
          }
  
  }
      // this is for the view
      function select_stream()
      {
          $this-&gt;db-&gt;select('*');
          $this-&gt;db-&gt;from('pf_stream');
          $this-&gt;db-&gt;join('pf_users', 'pf_users.id = pf_stream.added_by');
          $this-&gt;db-&gt;order_by('pf_stream.sid', 'DESC');
          $this-&gt;db-&gt;limit(5);
          $query = $this-&gt;db-&gt;get();
  
          if($query-&gt;num_rows() &gt; 0)
          {   
              foreach($query-&gt;result() as $row)
              {
                  $data[] = $row;
              }
          return $data;   
          }
      }
  </code></pre>
  
  <p>Controller</p>
  
  <pre><code> public function index()
     {
          $this-&gt;load-&gt;view('partials/header');
          $data['stream_posts'] = $this-&gt;stream_model-&gt;select_stream();
             $this-&gt;load-&gt;view('stream_view', $data);
          $this-&gt;load-&gt;view('partials/footer');
     }
  
      function insert_stream()
      {
          $this-&gt;form_validation-&gt;set_rules('stream_text', 'Üzenet', 'trim|required|xss_clean');
  
          if($this-&gt;form_validation-&gt;run() == false)
          {
              $stream = array('error' =&gt; 1);
          } else {
              $this-&gt;stream_model-&gt;add_stream();
              $data['stream_result'] = $this-&gt;stream_model-&gt;load_stream_post();
              $stream = array(
                 'error' =&gt; 0, 
                 'sid' =&gt; $data['stream_result'][0]-&gt;sid,
                 'text' =&gt; $data['stream_result'][0]-&gt;text,
                 'author' =&gt; $data['stream_result'][0]-&gt;first_name . " " . $data['stream_result'][0]-&gt;last_name,
              );
          }
          echo json_encode($stream);
      }
  </code></pre>
  
  <p>Could someone please give me a hint what am I missing?</p>