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


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

Form (it's practically the search bar of Magento, but modified)

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


In this same phtml file I wan't to display the search_word, I tried with a modal window but it's not showing anything, in other hand if it redirects to a different page it shows the search_word correctly.


  • Can you please upload your form code ? – Rohan Hapani Nov 12 '19 at 16:32
  • Done, please see updated question. – Oscar Vazquez Nov 12 '19 at 16:45
  • what is your controller path? – Rohan Hapani Nov 12 '19 at 16:50
  • it's result/index/readsearch, so after submit search form, it redirects me to result/index/readsearch and the data is shown successfully, but when I try to keep the data in the same page, it does not. – Oscar Vazquez Nov 12 '19 at 16:58

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.