I'm a little bit stuck. I want to send a form and display it in a phtml.
Here is my code:
1. Controller Search.php
<?php namespace Vendor\SearchStores\Controller\Index; use Magento\Framework\App\Action\Action; use Magento\Framework\App\Action\Context; use Magento\Framework\View\Result\PageFactory; use Magento\Framework\Registry; use Magento\Framework\Controller\Result\JsonFactory; //use Magento\Framework\Controller\Result\Forward\Factory; class ReadSearch extends Action { protected $resultPageFactory; protected $resultJsonFactory; protected $_coreRegistry; public function __construct(Context $context, PageFactory $pageFactory, Registry $coreRegistry, JsonFactory $resultJsonFactory) { $this->resultPageFactory = $pageFactory; $this->_coreRegistry = $coreRegistry; $this->resultJsonFactory = $resultJsonFactory; parent::__construct($context); } public function execute() { $search_word = $this->getRequest()->getParam('search'); // $search_word = $this->getRequest()->getPost('search'); $resultPage = $this->resultPageFactory->create(); $result = $this->resultJsonFactory->create(); $block = $resultPage->getLayout() ->createBlock('Vendor\SearchStores\Block\ShowResult') ->setTemplate('Vendor_SearchStores::results_modal.phtml') ->setData('search',$search_word) ->toHtml(); $result->setData(['output' => $block]); return $result; } } 2. Block ShowResult.php
<?php namespace Vendor\SearchStores\Block; use Magento\Framework\View\Element\Template\Context; use Magento\Framework\View\Element\Template; use Magento\Framework\Registry; class ShowResult extends Template { protected $_coreRegistry; public function __construct(Context $context, Registry $coreRegistry) { $this->_coreRegistry = $coreRegistry; parent::__construct($context); } public function getSearchWord() { //HERE IS WHERE I WANT TO GET THE DATA FROM THE CONTROLLER return $this->getSearch(); } } 3. xml result_index_readsearch.xml
<?xml version="1.0"?> <page layout="1column" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceContainer name="content"> <block class="Vendor\SearchStores\Block\ShowResult" name="show_result" template="Vendor_SearchStores::results_modal.phtml"/> </referenceContainer> </body> </page> 4. Template results_modal.phtml
<?php echo $block->getSearchWord(); ?> NOTE: I can get info in the controller but I don't know how to send it to Block. I try to display in template, but it seems that when I send the form, and display template, the form arrives before loading the template, so I see no data in template. Please suggest a solution.
Thanks!!