0

I am working on list of categories where I need to select one of root categories in the list and after selecting it, it should show the subcategories and so on. SO I am here using ajax calls. As in ajax I am passing variables using get method but how can I a call block.

I have checked in controller for testing and its working fine using this url in ajax as using in controller

xmlhttp.open("GET","http://localhost/magento/weblog/index/fn/?id="+str,true);

then tried to implement on block but i am not able figure out regarding the URL in blocks. How can I use it in Block. and I thik we can not pass the variables from controller to blocks. So finally my question is, How can use Ajax in blocks so that I can use this function in phtml file

Thanks everyone :)

  • Magento already doe something like that. Check how the category widget chooser works. You can see the behavior by trying to add in a cms page a 'Category Link' widget. You can see the code in Mage_Adminhtml_Catalog_Category_WidgetController and Mage_Adminhtml_Block_Catalog_Category_Widget_Chooser. Or directly in the category tree in admin, but that seams a little more complicated to understand. – Marius Jun 5 '13 at 11:00
2

You need a controller to access the block. In the controller you then call the block with:

$this->loadLayout()
->_addContent($this->getLayout()->createBlock('[module_alias]/[block]'))
->renderLayout();

You can also pass the needed url parameter to you block if you use in the code above:

$this->getLayout()
->createBlock('[module_alias]/[block]')
->setRequestId($this->getRequest()->getParam('id')

The block then processes the request and outputs the data, the output can be formatted as HTML or Json depending on your needs.

  • Thanks. This is very good learning for me. I have a block already and created function to get the rot categories in drop down box and then selection of any one root categories I am sending ajax request to a another function in block to get the subcategories. However, I am not able to figure out how can then access the another function after selecting root categories. – MagentoBoy Jun 5 '13 at 11:43
  • Thanks for answer. Can you please give me any examples in which we have one dropdown box. on the onChnage event, it will call ajax and then it will create another dropdown and then again same process which is in the same block. Can you please give me example according to your hint. Thank you in advance :) – MagentoBoy Jun 20 '13 at 12:43

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.