Magento Support Team is correct, but it's not quite a complete answer - in your controller you need to first load the layout:
$this->loadLayout();
now you can get your block and set a value:
$this->getLayout()->getBlock('yourblockname')->setValue($somevar);
and finally render it out:
$this->renderLayout();
in your block you can then:
$this->getValue();
which will return $somevar.
The other alternative is to set a registry value in your controller:
Mage::register('name', $value);
and retrieve it in your block:
Mage::registry('name');
you can delete the registry value with:
Mage::unregister('name');
note that if you try to register a value that already exists an exception will be thrown so if in doubt unregister before you register (you can unregister a non existent registry value without causing issues).
With an AJAX request you are either going to want to set the response body in your controller to determine what is returned in the responseText:
$this->getResponse()->setBody('some repsonse');
or you are going to want to replace the root block in your layout with just your block in the layout so that the response doesn't contain common areas like header, footer and sidebar and instead just contains your blocks content (this way you don't need to set the response body in the controller):
<?xml version="1.0"?>
<layout version="0.1.0">
<frontname_controller_action>
<block type=module/block" name="root" template="module/template.phtml" />
</frontname_controller_action>
</layout>
If you do it this way, you just need to ->getBlock('root')
in the controller when pulling the block to set a value against it.
EDIT: As a side note, you shouldn't be closing your PHP classes with ?>
- see here
Please follow below steps.
Step-1: create select box element like below code in your ui_component form xml
<field name="select_box_1">
<argument name="data" xsi:type="array">
<item name="options" xsi:type="object">Namespace\Module\Model\Config\Source\SelectBoxOption</item>
<item name="config" xsi:type="array">
<item name="dataType" xsi:type="string">text</item>
<item name="label" xsi:type="string" translate="true">Select Option</item>
<item name="component" xsi:type="string">Namespace_Module/js/form/element/select-option</item>
<item name="formElement" xsi:type="string">select</item>
<item name="source" xsi:type="string">modulename</item>
<item name="dataScope" xsi:type="string">select_box_1</item>
<item name="validation" xsi:type="array">
<item name="required-entry" xsi:type="boolean">true</item>
</item>
</item>
</argument>
</field>
<field name="select_box_2">
<argument name="data" xsi:type="array">
<item name="options" xsi:type="object">Namespace\Module\Model\Config\Source\SelectBoxTwoOption</item>
<item name="config" xsi:type="array">
<item name="dataType" xsi:type="string">text</item>
<item name="label" xsi:type="string" translate="true">Select Option</item>
<item name="formElement" xsi:type="string">select</item>
<item name="source" xsi:type="string">modulename</item>
<item name="dataScope" xsi:type="string">select_box_2</item>
<item name="validation" xsi:type="array">
<item name="required-entry" xsi:type="boolean">true</item>
</item>
</item>
</argument>
</field>
Step-2: create select-option.js in Namespace/Module/view/adminhtml/web/js/form/element and paste below code in file:
define([
'jquery',
'underscore',
'uiRegistry',
'Magento_Ui/js/form/element/select',
'Magento_Ui/js/modal/modal',
], function ($, _, uiRegistry, select, modal) {
'use strict';
return select.extend({
/**
* On value change handler.
*
* @param {String} value
*/
onUpdate: function (value)
{
if (value != 'undefined')
{
//Do your Ajx stuff here
}
return this._super();
},
});
});
Best Answer
1. Register new module:
app/etc/modules/SeStro_AjaxProductInfo.xml
2. Add new router:
app/code/local/SeStro/AjaxProductInfo/etc/config.xml
app/code/local/SeStro/AjaxProductInfo/controllers/IndexController.php
3. And modify your templatefile: