AJAX requests in Magento

April 10th, 2012 by Laeeq | 1 comment

Sometimes its very troublesome to impliment Ajax with Magento. Actaully you will have to couple controllers and layout. Here in this post , I have explained few simple step to fix this issues.

Let’s first know in Magento terms what we need.

Controller: This is a request url. You will have to set up the controller with its frontend router set in config.xml and its corresponding controller class.

Layout: Layout will handle the requested URL and return HTML.

Block: The block to call through layout for the above controller.

Make Ajax Call:

  1. var loadurl = ‘<?php echo $this->getUrl(‘ACTIONPATH‘) ?>’;
  2. Element.show(‘MYPANEL’);
  3. new Ajax.Request(loadurl, {
  4. method: ‘post’,
  5. parameters: “Params_Here”,
  6. onComplete: function(transport) {
  7. Element.hide(‘MYPANEL’);
  8. $(‘output-div’).innerHTML = “”;
  9. $(‘output-div’).innerHTML = transport.responseText;
  10. }
  11. });

After the Ajax Call is made it goes to your controller’s action, which in turn sees to your layout as follows:

  1. class Namespace_module_frontendController extends Mage_Core_Controller_Front_Action
  2. {
  3. public function actionAction(){
  4. $this->loadLayout()->renderLayout();
  5. }
  6. }

As in layout we will have to define reference where the html will be displayed. you can make changes according to your requirement to reference properties;

  1. <module_controller_action>
  2. <block type=“module/block” name=“root” output=“toHtml” template=“module/template.phtml”/>
  3. </module_controller_action>

Hope it will work for you!

You can subscribe to PHPZAG.COM posts by Email

 

Related Topics:

  • Get an array of billing addresses and shipping addresses in Magento
  • Duplicate Data Issue with Custom Magento Collection
  • Tips To Improve Your Ecommerce Site’s Security
  • Magento: Display New Products from Specific Category On Home Page
  • Dealing with Magento Model or Collections
  • Magento: Display New Products On Home Page
  • Magento: Display More Than 5 New Products
  • Override Controllers in Magento
  • Improve the performance of your Magento store
  • Magento Released Community Edition 1.7.0.1
  • Write custom title, keywords and description in Magento module
  • Moving Magento site from development to live server
  • Display Related products on product details page in Magento
  • Magento – Models, resource models, and collections
  • Magento – Add thumbnail images to Magento admin grid
  • Simple url rewrite in magento
  • Generate CSV file in Magento
  • Resize image in Magento using Varien_Image class
  • Category Navigation Listings in Magento
  • Magento- Create a Drop-Down of Countries
  •  

     

    1. averyrichardsondo
      September 28th, 2012 at 09:59 | #1

      I’ll right away grab your rss feed as I can’t in finding your email subscription link or newsletter service. Do you’ve any? Please permit me know so that I may just subscribe. Thanks.

    1. No trackbacks yet.