0

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.

Controller

public function execute()
    {


       $searchWord = $this->getRequest()->getParam('search');
        if ($this->_coreRegistry->registry('search_word')) {
            $this->_coreRegistry->unregister('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()
            ->createBlock('Vendor\SearchStores\Block\ShowResult')
            ->setTemplate('Vendor_SearchStores::results_modal.phtml')
            ->toHtml();
        $result->setData(['output' => $block]);

        return $resultPage;


    }

Block

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

    <body>

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

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>
            </label>
            <div class="control">
                <input id="search"
                       data-mage-init='{"quickSearch":{
                            "formSelector":"#search_mini_form",
                            "url":"<?= /* @escapeNotVerified */ $helper->getSuggestUrl()?>",
                            "destinationSelector":"#search_autocomplete"}
                       }'
                       type="text"
                       name="search"
                       value="<?= /* @escapeNotVerified */ $helper->getEscapedQueryText() ?>"
                       placeholder="<?= /* @escapeNotVerified */ __('Search entire store here...') ?>"
                       class="input-text"
                       maxlength="<?= /* @escapeNotVerified */ $helper->getMaxQueryLength() ?>"
                       role="combobox"
                       aria-haspopup="false"
                       aria-autocomplete="both"
                       autocomplete="off"/>
                <div id="search_autocomplete" class="search-autocomplete"></div>
                <?= $block->getChildHtml() ?>
            </div>
        </div>
        <div class="actions">
            <button 
                    id = "submit-search"
                    type="submit"
                    title="<?= $block->escapeHtml(__('Search')) ?>"
                    class="action search">
                <span><?= /* @escapeNotVerified */ __('Search') ?></span>
            </button>
        </div>
        </form> 
</div>

Greetings!

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