title: CodeIgniter Return HTML in JSON Response
body: <p>I'm trying to build an ajax-powered shopping cart in codeigniter, and now I'm facing a problem with how to get the response as a HTML, and encode it as a JSON response, then append the shopping cart page with the response.</p>
  
  <p>Here is the javascript code:</p>
  
  <pre><code>$('.addtocart').on('click', function (event) {
      event.preventDefault();
      var AddedQty = parseInt($('#attr-quantity').val());
  
      $('#shoppingcart div.cart, #shoppingcart div.loading').remove();
      $('#shoppingcart').append('&lt;div class="loading"&gt;&lt;/div&gt;');
      shoppingcart.open();
  
      $.post('/mywebsite/cart/add', {
          itemId: $('.addtocart').data('itemId'),
          quantity: AddedQty
      }, function (response) { 
          var html = $(response.ShoppingCartHtml).html(); 
          shoppingcart.update(html);
          shoppingcart.close();
      });
  });
  </code></pre>
  
  <p>And this is the code for the cart controller:</p>
  
  <pre><code>public function add() {     
      $this-&gt;load-&gt;model('cart_model');
      $id = $this-&gt;input-&gt;post('itemId');
      $qty = $this-&gt;input-&gt;post('quantity');
      $cart = $this-&gt;cart-&gt;contents();
      $exists = false;
      $rowid = '';
  
      foreach ($cart as $item) {
          if ($item['id'] == $id) {
              $exists = true;
              $rowid = $item['rowid'];
              $qty = $item['qty'] + $qty;
          }
      }
  
      if ($exists) {
          $this-&gt;cart_model-&gt;update_item($rowid, $qty);          
      } else {
          $this-&gt;cart_model-&gt;add_cart_item();
      }
  
      $this-&gt;output-&gt;set_content_type('application/json');
      $this-&gt;output-&gt;set_output(json_encode(array('ShoppingCartHtml'=&gt; $theHTMLResponse)));
  }
  </code></pre>
  
  <p>And the code below is the sample code (not the real code) that I want to encode it as the JSON response, as the ShoppingCartHtml:</p>
  
  <pre><code>&lt;li&gt;
      &lt;h3&gt;&lt;?php echo $ProductName; ?&gt;&lt;/h3&gt;
  &lt;/li&gt;
  </code></pre>
  
  <p>So far, I've tried to echo the view, and encode it using the json_encode, but I get an error.
  Here is what I've come with:</p>
  
  <pre><code>$theHTMLResponse= echo $this-&gt;load-&gt;view('pages/minicart.php', $data); //THIS LINE THROWS ERROR (I know that I cannot assign what echo-ed into a variable).
  $this-&gt;output-&gt;set_content_type('application/json');
  $this-&gt;output-&gt;set_output(json_encode(array('ShoppingCartHtml'=&gt; $theHTMLResponse)));
  </code></pre>
  
  <p>The example of the correct response that we wanted is like the codes below (as shown in Firebug) :</p>
  
  <pre><code>{"MinicartHtml":"\u003cli\u003e\r\n\u003ch3\u003eThe Product Name\u003c/h3\u003e\u003c/li\u003e"}
  </code></pre>
  
  <p>Which if I inspected in the Firebug console, on the JSON tab, it should shows the ShoppingCartHtml's html codes, enclosed with quotes, as the ShoppingCartHtml JSON Response.</p>
  
  <p>The question is: How can I encode the ShoppingCartHtml's html codes as a JSON response?</p>
  
  <p>PS: My apologize if my question is confusing. English is not my cup of tea. Even to type this question, I need almost 1 hour to complete it. But I hope you guys understand what I asked.</p>
  
  <p>Thank you in advance.</p>