I got help to accomplish send data from Controller to a Block, and then display the info from Block in a template.

The thing is that I want to send a form and display the data from that form in the same form fields. Do I need to use Ajax or jQuery for this? For the moment when I submit the form, the controller returns the ResultPageFactory and I'm automatically redirected to frontname/controller/action route, I want to stay in the same page but with the data sent to the controller.


public function execute()

       $searchWord = $this->getRequest()->getParam('search');
        if ($this->_coreRegistry->registry('search_word')) {
        // set value to _coreRegistry variable  search_word
        $this->_coreRegistry->register('search_word', $searchWord);
        $resultPage = $this->resultPageFactory->create();
        $result = $this->resultJsonFactory->create();
        $block = $resultPage->getLayout()
        $result->setData(['output' => $block]);

        return $resultPage;



public function getSearchWord()

         $searchWord = '';
        if ($this->_coreRegistry->registry('search_word')) {
            $searchWord = $this->_coreRegistry->registry('search_word');

        return $searchWord;


XML layout

<?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">


        <referenceContainer name="content">
            <block class="Vendor\SearchStores\Block\ShowResult" name="show_result" template="Vendor_SearchStores::results_modal.phtml"/>

Template search_bar.phtml

<div class="block block-search">
    <div class="block block-title"><strong><?= /* @escapeNotVerified */ __('Search') ?></strong></div>
    <div class="block block-content">

 <form class="form minisearch" id="search_mini_form" action="<?= $block->getUrl('result/index/readsearch'); ?>" method = "POST"> 

        <div class="field search">
            <label class="label" for="search" data-role="minisearch-label">
                <span><?= /* @escapeNotVerified */ __('Search') ?></span>
            <div class="control">
                <input id="search"
                            "url":"<?= /* @escapeNotVerified */ $helper->getSuggestUrl()?>",
                       value="<?= /* @escapeNotVerified */ $helper->getEscapedQueryText() ?>"
                       placeholder="<?= /* @escapeNotVerified */ __('Search entire store here...') ?>"
                       maxlength="<?= /* @escapeNotVerified */ $helper->getMaxQueryLength() ?>"
                <div id="search_autocomplete" class="search-autocomplete"></div>
                <?= $block->getChildHtml() ?>
        <div class="actions">
                    id = "submit-search"
                    title="<?= $block->escapeHtml(__('Search')) ?>"
                    class="action search">
                <span><?= /* @escapeNotVerified */ __('Search') ?></span>


  • 1
    Which is the form controller and which the post controller? If you use the same block you always could redirect to referrer url and not return a result page in your post controller and go back to your form. – pontikos Nov 12 '19 at 19:22
  • I'm using just one controller, it's Vendor\SearchStores\Controller\Index and the path is result/index/readsearch, it´s the controller that receives the post form, please check edited question – Oscar Vazquez Nov 12 '19 at 19:32
  • Can you explain how to do this in the Block? If I don't return result page in post controller then what should I use it for? – Oscar Vazquez Nov 12 '19 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.