2

My Controller File

public function myAction() {


        $response = array();
        $response['myresponse'] = $this->myAjax();
        $this->getResponse()->clearHeaders()->setHeader('Content-type','application/json',true);
        return $this->getResponse()->setBody(json_encode($response));
    }



    protected function myAjax()
    {

        $layout = $this->getLayout();
        $totalsBlock = $layout->createBlock('checkout/cart_totals')->setTemplate('checkout/cart/totals.phtml');
        return $totalsBlock->toHtml();
    }

My JSON Response

{"myresponse":" <table id=\"shopping-cart-totals-table\">\n <col \/>\n <col width=\"1\" \/>\n <tfoot>\n <tr>\n <td style=\"\" class=\"a-right\" colspan=\"1\">\n <strong>Grand Total<\/strong>\n <\/td>\n <td style=\"\" class=\"a-right\">\n <strong><span class=\"price\">$90.56<\/span><\/strong>\n <\/td>\n<\/tr>\n <\/tfoot>\n <tbody>\n <tr>\n <td style=\"\" class=\"a-right\" colspan=\"1\">\n Subtotal <\/td>\n <td style=\"\" class=\"a-right\">\n <span class=\"price\">$820.56<\/span> <\/td>\n<\/tr>\n<tr>\n <td style=\"\" class=\"a-right\" colspan=\"1\">\n Shipping &amp; Handling (Flat Rate - Fixed) <\/td>\n <td style=\"\" class=\"a-right\">\n <span class=\"price\">$90.00<\/span> <\/td>\n<\/tr>\n<tr>\n <\/tr>\n <\/tbody>\n <\/table>\n"}

My Oncomplete function

new Ajax.Request("<?php echo $formAction;?>", {
           method: 'post',
           postBody: "mypostdata="+$('my_value').value,
           onComplete: function(data) {
                var mydata = data.responseText.evalJSON(true);
                $('shopping-cart-totals-table').update(mydata);
            }
    });

The cart totals block is being updated but with incorrect HTML format. How could I make it in formatted data and show proper HTML in cart totals block ?

Please Help.

Thanks.

1

Instead of using

$('shopping-cart-totals-table').update(mydata);

Try using

$('shopping-cart-totals-table').update(mydata.myresponse);

You are sending JSON with the key myresponse and your cart block html as the value for that key. So mydata will be an Object after json is converted into a JS object and this object should have a property myresponse with the HTML for the cart block.

  • Paras, you seem logical, but how can I pass multiple parameters in postBody. Will it create any issue if I use your code ? – Jordan Aug 26 '14 at 6:33
  • You don't have to send multiple params in postBody. Just use $('shopping-cart-totals-table').update(mydata.myresponse); in the oncomplete function. – Paras Sood Aug 26 '14 at 6:43
  • Okay, but what if I need to send multiple params ? Is it possible in my posted code ? – Jordan Aug 26 '14 at 6:44
  • Please post that as a separate question. :) – Paras Sood Aug 26 '14 at 6:58
  • Thanks, posted pls check magento.stackexchange.com/questions/33568/… – Jordan Aug 26 '14 at 6:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.