Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeIgniter Return HTML in JSON Response

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.

Here is the javascript code:

$('.addtocart').on('click', function (event) {
    event.preventDefault();
    var AddedQty = parseInt($('#attr-quantity').val());

    $('#shoppingcart div.cart, #shoppingcart div.loading').remove();
    $('#shoppingcart').append('<div class="loading"></div>');
    shoppingcart.open();

    $.post('/mywebsite/cart/add', {
        itemId: $('.addtocart').data('itemId'),
        quantity: AddedQty
    }, function (response) { 
        var html = $(response.ShoppingCartHtml).html(); 
        shoppingcart.update(html);
        shoppingcart.close();
    });
});

And this is the code for the cart controller:

public function add() {     
    $this->load->model('cart_model');
    $id = $this->input->post('itemId');
    $qty = $this->input->post('quantity');
    $cart = $this->cart->contents();
    $exists = false;
    $rowid = '';

    foreach ($cart as $item) {
        if ($item['id'] == $id) {
            $exists = true;
            $rowid = $item['rowid'];
            $qty = $item['qty'] + $qty;
        }
    }

    if ($exists) {
        $this->cart_model->update_item($rowid, $qty);          
    } else {
        $this->cart_model->add_cart_item();
    }

    $this->output->set_content_type('application/json');
    $this->output->set_output(json_encode(array('ShoppingCartHtml'=> $theHTMLResponse)));
}

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:

<li>
    <h3><?php echo $ProductName; ?></h3>
</li>

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:

$theHTMLResponse= echo $this->load->view('pages/minicart.php', $data); //THIS LINE THROWS ERROR (I know that I cannot assign what echo-ed into a variable).
$this->output->set_content_type('application/json');
$this->output->set_output(json_encode(array('ShoppingCartHtml'=> $theHTMLResponse)));

The example of the correct response that we wanted is like the codes below (as shown in Firebug) :

{"MinicartHtml":"\u003cli\u003e\r\n\u003ch3\u003eThe Product Name\u003c/h3\u003e\u003c/li\u003e"}

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.

The question is: How can I encode the ShoppingCartHtml's html codes as a JSON response?

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.

Thank you in advance.

like image 841
Paradiso Trickster Avatar asked Sep 09 '13 11:09

Paradiso Trickster


1 Answers

You were close to getting it right. Just a couple of adjustments needed.

//set the 3rd param to true to make it return data 
$theHTMLResponse    = $this->load->view('path/to/view.php', null, true);

$this->output->set_content_type('application/json');
$this->output->set_output(json_encode(array('ShoppingCartHtml'=> $theHTMLResponse)));
like image 92
JohnP Avatar answered Oct 16 '22 14:10

JohnP