Question

I'm creating a custom form in Magento 2.3 admin.

Vendor/Module/view/adminhtml/layout/route_folder_index.xml

<?xml version="1.0"?>
<page 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\Module\Block\Adminhtml\NewFunction\Edit" name="newfunction.form.container" template="newfunction.phtml"/>
        </referenceContainer>
    </body>
</page>

Vendor/Module/Block/Adminhtml/NewFunction/Edit.php

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

/**
 * Export edit block
 *
 * @author      Magento Core Team <core@magentocommerce.com>
 */
namespace Vendor\Module\Block\Adminhtml\NewFunction;

/**
 * @api
 * @since 100.0.2
 */
class Edit extends \Magento\Backend\Block\Template
{
    public function getNewFunctionUrl()
    {
        return $this->getUrl('route/folder/newfunction');
    }

}

Vendor/Module/view/adminhtml/templates/newfunction.phtml

<form action="<?php echo $block->getNewFunctionUrl()?>" method="post" enctype="multipart/form-data">
    <input type="text" name="hello"/>
    <input type="submit" value="Submit" />
</form>

Vendor/Module/Controller/Adminhtml/Folder/NewFunction.php

<?php

namespace Vendor\Module\Controller\Adminhtml\Folder;


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

    public function execute()
    {

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

    }
}

The form submits perfectly but when I debug the $params variable it shows only two parameters

key
adminhtml

I was expecting the hello text as one of the parameter but didn't receive one on form submit.

Is there anything wrong with the process or code?

Was it helpful?

Solution

Your controller should extend Magento\Backend\App\Action and implements Magento\Framework\App\Action\HttpPostActionInterface, also don't forget about ACL.

So should look something like this:

namespace Vendor\Module\Controller\Adminhtml\Folder;

class NewFunction extends \Magento\Backend\App\Action implements \Magento\Framework\App\Action\HttpPostActionInterface
{
    /**
     * Authorization level of a basic admin session
     *
     * @see _isAllowed()
     */
    public const ADMIN_RESOURCE = 'Magento_Cms::save';

    public function execute()
    {
        $params = $this->getRequest()->getParams();
        //OR
        $name = $this->getRequest()->getParam('hello'); 
    }
}

Also don't forget about form_key input, your view should look like:

<form action="<?php echo $block->getNewFunctionUrl()?>" method="post" enctype="multipart/form-data">
    <input type="hidden" name="form_key" value="<?= $block->getFormKey() ?>">
    <input type="text" name="hello"/>
    <input type="submit" value="Submit" />
</form>

OTHER TIPS

You need to try code like this one.

<?php
namespace Vendor\Module\Controller\Adminhtml\Folder;

class NewFunction extends \Magento\Backend\App\Action
{
    public function execute()
    {
        $params = $this->getRequest()->getParams();
        //OR
        $name = $this->getRequest()->getParam('hello'); 
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top