Question

I created my own admin route. This is it's controller:

Route controller

<?php


class Mediabase extends \Magento\Sales\Controller\Adminhtml\Order
{   
    public function execute()
    {     

        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $urlInterface = $objectManager->get(\Magento\Backend\Model\UrlInterface::class);
        $mediabaseUrl = $urlInterface->getUrl("uo_mediabase/order/mediabase");

        print_r($this->getRequest()->getPost());
        $currentUrl = $urlInterface->getCurrentUrl();
    }
}
?>     

This is the form which is calling the route

<div class="admin__page-section order-view-billing-shipping">
    <div class="admin__page-section-title">
        <span class="title">Mediabase</span>
    </div>

    <div class="admin__page-section-item-title">
        <span class="title">Bestellnummer</span>
    </div>

    <div class="admin__page-section-item-content">
        Mediabase-Bestellnummer: <?php echo $block->getMediabaseNumber(); ?>
    </div>
    <br/>   
    <form id="mediabase_edit_form" action="<?= $this->getFormUrl() ?>" method="post">
        <input type="text" name="bestellnr"><br/>
        <input class="action-default" type="submit" value="Speichern">
    </form>
</div>

So when the route is called I want to get the

bestellnr

value.

However my form doesn't seem to send POST data.

Do you know how I can achieve that?

Thanks!

Était-ce utile?

La solution

First check your controller is being called on form submit or not if it's being called then follow below method

If you want to get post data from controller, $post = $this->getRequest()->getPostValue();

Here your full code,

Also You have to declare storemanager object inside __construct() function of your php file instead of use dirctly objectmanager.

I have updated your code as below,

class Index extends \Magento\Framework\App\Action\Action
{

    /**
     * @var \Magento\Framework\App\Cache\TypeListInterface
     */
    protected $_cacheTypeList;

    /**
     * @var \Magento\Framework\App\Cache\StateInterface
     */
    protected $_cacheState;

    /**
     * @var \Magento\Framework\App\Cache\Frontend\Pool
     */
    protected $_cacheFrontendPool;

    /**
     * @var \Magento\Framework\View\Result\PageFactory
     */
    protected $resultPageFactory;

    /**
     * @param Action\Context $context
     * @param \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList
     * @param \Magento\Framework\App\Cache\StateInterface $cacheState
     * @param \Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
     */
    public function __construct(
       \Magento\Framework\App\Action\Context $context,
       \Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
       \Magento\Framework\App\Cache\StateInterface $cacheState,
       \Magento\Framework\App\Cache\Frontend\Pool $cacheFrontendPool,
       \Magento\Framework\View\Result\PageFactory $resultPageFactory,
       \Magento\Store\Model\StoreManagerInterface $storeManager
    )
    {
        parent::__construct($context);
        $this->_cacheTypeList = $cacheTypeList;
        $this->_cacheState = $cacheState;
        $this->_cacheFrontendPool = $cacheFrontendPool;
        $this->resultPageFactory = $resultPageFactory;
        $this->storeManager = $storeManager;
    }

    /**
     * Flush cache storage
     *
     */
    public function execute()
    {
        $currentStore = $this->storeManager->getStore();
        $baseUrl = $currentStore->getBaseUrl();

        $post = $this->getRequest()->getPostValue();

        echo "<pre>";
        print_r($post);
        exit;

    }
}

Also, you can check here:

http://clever-code.com/how-to-save-form-data-to-the-custom-table-in-magento-2/

Autres conseils

You can use the following code to get post or get values from form to controller action:

Below code will return all the parameters either post or get:

$params = $this->getRequest()->getParams();

If you want only one variable value then you can use the following code:

$variable_name = $this->getRequest()->getParam('variable_name');

In your case you can use the following code to get bestellnr value:

$bestellnr = $this->getRequest()->getParam('bestellnr');
Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange
scroll top