rss
twitter
    Find out what I'm doing, Follow Me :)

Thursday, December 3, 2009

Pin It

Widgets

Working with AJAX in Magento

Ajax in Magento can be pretty troublesome.Because you will need to take controllers and layout into account.And I almost used up a whole day trying to make ajax work. Here are some of the steps I’d like to share so that you will not waste lots of your time, before making this work.
Let’s first know in Magento terms what we need.

Controller: The url on which Ajax will work on, or the 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 to handle the requested URL and return HTML if required.
Block: The block to call through layout for the above controller.

Now lets be descriptive.

If you want to show a loader while request is being processed just add the following whereever you like in the phtml page.First fetch the Javascript.

<script src="<?php echo $this->getJsUrl() ?>mage/adminhtml/loader.js" type="text/javascript"></script> 

Then echo the loader which pops up. This loader will be similar to that of the admin.

<div id="loadingmask" style="display: none;"> 
<div class="loader" id="loading-mask-loader"><img src="<?php echo str_replace("index.php/","",$this->getUrl()) ?>skin/adminhtml/default/default/images/ajax-loader-tr.gif" alt="<?php echo $this->__('Loading...') ?>"/><?php echo $this->__('Loading...') ?></div> 
<div id="loading-mask"></div> 
</div>

Now to Call Ajax, use the following lines

/*Please note that the URL is created in reloadurl. Also see that the response text will be echoed in div with id=output-div*/ 
   
var reloadurl = '<?php echo $this->getUrl('router/controller/action') ?>'; 
Element.show('loadingmask'); 
new Ajax.Request(reloadurl, { 
method: 'post', 
parameters: "Params_Here", 
onComplete: function(transport) { 
Element.hide('loadingmask'); 
$('output-div').innerHTML = ""; 
$('output-div').innerHTML = transport.responseText; 
  

});

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

class Namespace_module_frontendController extends Mage_Core_Controller_Front_Action  

public function actionAction(){ 
$this->loadLayout()->renderLayout(); 

}

And here is the main part in layout where I spent most of my time in. Since in layout we will have to define reference where the html will echo (Most of the times), i got stuck here, because i need to echo the output on the div with id output-div not in any reference.And the trick is to name the layout as root and output as html. Like the following:

<module_controller_action> 
<block type="module/block"  name="root" output="toHtml"  template="module/template.phtml"/> 
</module_controller_action>

You are done now! What ever you write or echo in phtml file you will see populated in the DIV. Now you can treat block as normally as you do before.

Source: http://subesh.com.np/

4 comments:

  1. Thanks a lot for your post.
    I am looking code for rendering specific block with ajax for a long time.

    ReplyDelete
  2. Hi,

    If i want to post full form values what should be parameters ?

    Regards,
    Kam

    ReplyDelete
  3. You Save my lots of time. Thanks

    ReplyDelete
  4. Hello. Thanks for the useful information. You’re welcome to check out our following selection in the article on the similar topic posted here http://www.atwix.com/magento/ajax-requests-in-magento/
    We will be glad to see your feedback.

    ReplyDelete