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 :D.
[UPDATE]
Get Ajaxify Extension from Magento Connect to work with AJAX on Magento
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.
[source language=”javascript”]
<script src="<?php echo $this->getJsUrl() ?>mage/adminhtml/loader.js" type="text/javascript"></script>
[/source]
Then echo the loader which pops up. This loader will be similar to that of the admin.
[source language=”html”]
<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>
[/source]
Now to Call Ajax, use the following lines
[source language=”javascript”]
/*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;
}
});
[/source]
After the Ajax Request is made it goes to your controller’s action, which in turn sees to your layout as follows:
[source language=”php”]
class Namespace_module_frontendController extends Mage_Core_Controller_Front_Action
{
public function actionAction(){
$this->loadLayout()->renderLayout();
}
}
[/source]
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:
[source language=”XML”]
<module_controller_action>
<block type="module/block" name="root" output="toHtml" template="module/template.phtml"/>
</module_controller_action>
[/source]
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.
Happy Coding!!
[UPDATE]
Get Ajaxify Extension from Magento Connect to work with AJAX on Magento