title: AJAX doesn't update post when PHP code stored inside .html function
body: <p>I'm having a bit of confusion on why the PHP variables used in the html variable that I inserted inside the .ajax success function doesn't update my post automatically, but works fine when using the data parameter I supplied in the success function. </p>
  
  <p>The reason I prefer inserting data I got from my controller is I need to use some Codeigniter functions, which I cannot do with the latter. </p>
  
  <p>This version of my function where I utilized the data parameter is working fine, but I need to use the word_limiter() function from Codeigniter to reduce the post length</p>
  
  <p><strong>Version using data parameter</strong></p>
  
  <pre><code>function showFeed() {
              $.ajax({
                  type: 'ajax',
                  url: '&lt;?= base_url()?&gt;feed/showfeed',
                  async: false,
                  dataType: 'json',
                  success: function(data) {
                      var html = '';
                      var i;
                      for (i = 0; i &lt; data.length; i++) {
                          html += '&lt;div class="my-3"&gt;' +
                          '&lt;h2&gt;'+ data[i]['title'] + '&lt;/h2&gt;' +
                          '&lt;span class="post-date text-center px-2 py-1"&gt; Posted on ' + data[i]['created_at'] + ' &lt;/span&gt;' +
                          '&lt;p class="my-2"&gt;' + data[i]['body'] + '&lt;/p&gt;&lt;br&gt;' +
                          '&lt;div class="col-6 offset-3"&gt;' + 
                          '&lt;img class="post-image" src="&lt;?= site_url();?&gt;/assets/images/posts/' + data[i]['post_image'] + '"&gt;&lt;/div&gt;' + 
                          '&lt;div class="my-5"&gt;' +
                          '&lt;a class="btn btn-dark float-left mr-2" href="http://localhost/ci-socmed/post/' + data[i]['slug'] +'"&gt;Read More&lt;/a&gt;' +
                          '&lt;form action="http://localhost/ci-socmed/feed/delete/' + data[i]['id'] + '"method="post" accept-charset="utf-8"&gt;' +
                          '&lt;input type="submit" value="Delete" class="btn btn-danger float-left mr-2"&gt;' +
                          '&lt;/form&gt;' +
                          '&lt;a class="btn btn-primary text-white float-left" id="btnEdit" data-toggle="modal" data-target="#editModal" data="' + data[i]['id'] + '"&gt;Edit Post&lt;/a&gt;' +
                          '&lt;/div&gt;&lt;/div&gt;&lt;div class="clearfix"&gt;&lt;/div&gt;'
                      }
                      $('#nf-post').html(html);
                  },
                  error: function() {
                      alert('Error');
                  }
              });
          }
  </code></pre>
  
  <p><strong>Version with using data from Feed controller</strong></p>
  
  <pre><code>function showFeed() {
              $.ajax({
                  type: 'ajax',
                  url: '&lt;?= base_url()?&gt;feed/showfeed',
                  async: false,
                  dataType: 'json',
                  success: function() {
                      var html = '';
                      html += 
                      '&lt;?php foreach ($feed as $post): ?&gt;' +
                      '&lt;div class="my-3"&gt;' +
                      '&lt;h2&gt;&lt;?= $post['title'] ?&gt;&lt;/h2&gt;' +
                      '&lt;span class="post-date text-center px-2 py-1"&gt;Posted on &lt;?= $post['created_at'] ?&gt;&lt;/span&gt;' +
                      '&lt;p class="my-2"&gt;&lt;?= word_limiter($post['body'], 30) ?&gt;&lt;/p&gt;&lt;br&gt;' +
                      '&lt;div class="col-6 offset-3"&gt;' + 
                      '&lt;img class="post-image" src="&lt;?= site_url();?&gt;/assets/images/posts/&lt;?= $post['post_image'] ?&gt;"&gt;&lt;/div&gt;' + 
                      '&lt;div class="my-5"&gt;' +
                      '&lt;?php if($this-&gt;session-&gt;userdata('user_id') == $post['user_id']): ?&gt;' +
                      '&lt;a class="btn btn-dark float-left mr-2" href="http://localhost/ci-socmed/post/&lt;?= $post['slug']?&gt;"&gt;Read More&lt;/a&gt;' +
                      '&lt;form action="http://localhost/ci-socmed/feed/delete/&lt;?= $post['id'] ?&gt;"method="post" accept-charset="utf-8"&gt;' +
                      '&lt;input type="submit" value="Delete" class="btn btn-danger float-left mr-2"&gt;' +
                      '&lt;/form&gt;' +
                      '&lt;a class="btn btn-primary text-white float-left" id="btnEdit" data-toggle="modal" data-target="#editModal" data="&lt;?= $post['id'] ?&gt;"&gt;Edit Post&lt;/a&gt;' +
                      '&lt;?php endif; ?&gt;' +
                      '&lt;/div&gt;&lt;/div&gt;&lt;div class="clearfix"&gt;&lt;/div&gt;' +
                      '&lt;?php endforeach ?&gt;'
  
                      $('#nf-post').html(html);
                  },
                  error: function() {
                      alert('Error');
                  }
              });
          }
  </code></pre>
  
  <p><strong>JQuery function for editing post</strong></p>
  
  <pre><code>$(document).on('click', '#btnEdit', function() {
              var id = $(this).attr('data');
              $('#editModal').modal('show');
              $('#editModal').find('.modal-title').text('Edit Post');
              $('#modalForm').attr('action', '&lt;?= base_url() ?&gt;feed/update');
              $.ajax({
                  type: 'ajax',
                  method: 'get',
                  url: '&lt;?php echo base_url() ?&gt;feed/edit',
                  data: {
                      id: id
                  },
                  async: false,
                  dataType: 'json',
                  success: function(data) {
                      $('textarea[name=edit-body]').val(data.body);
                      $('input[name=post-id]').val(data.id);
                  },
                  error: function() {
                      alert('Could not Edit Data');
                  }
              });
          });
  </code></pre>
  
  <p><strong>Feed controller</strong></p>
  
  <pre><code>class Feed extends CI_Controller
  {
      public function index($page = 'index')
      {
          if (!file_exists(APPPATH . 'views/feed/' . $page . '.php')) {
              show_404();
          }
  
          $data['title'] = ucfirst($page);
          $data['feed'] = $this-&gt;Feed_mdl-&gt;get_feed();
  
          $this-&gt;form_validation-&gt;set_rules('post-text', '', 'required', array('required' =&gt; 'Your post is empty.'));
  
          if ($this-&gt;form_validation-&gt;run() === FALSE) {
              $this-&gt;load-&gt;view('templates/header');
              $this-&gt;load-&gt;view('feed/' . $page, $data);
              $this-&gt;load-&gt;view('templates/footer');
          }
      }
  }
  </code></pre>
  
  <p><strong>My showfeed() function</strong></p>
  
  <pre><code>public function showfeed() {
          $this-&gt;db-&gt;order_by('id', 'desc');
          $query = $this-&gt;db-&gt;get('feed');
          if($query-&gt;num_rows() &gt; 0){
              return $query-&gt;result();
          }else{
              return false;
          }
      }
  </code></pre>
  
  <p>When I'm using the version which retrieves data from the Feed controller, the database updates it except that I need to refresh the page in order to see whatever changes I made to my post, unlike the other version.</p>
  
  <p><strong>EDIT</strong><br>
  Just checked that I put the wrong code on the second showfeed() function, where I used the <code>$data['feed]</code> object from the Feed controller, in which if I used this version of showfeed(), I'm reloading the page manually in order to view changes I've made in my post. Sorry for the confusion. </p>