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

<body>

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

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

>

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.

Greetings!

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