Abdul's comment is right, I forget the form_key, but simply use $block->getFormKey()
will not generate the form_key in phtml file. We need create the method in our block.
What is form_key
Form keys in Magento are a means of preventing against Cross Site Request Forgery, it instructs Magento to output and store a unique form key for a user session. All CSRF-protected Magento controller actions will verify against this before doing anything of value.
Create the getFormKey() in our block
<?php
namespace Vendor\Module\Block\Adminhtml\Index;
use Magento\Framework\Data\Form\FormKey;
use Magento\Framework\View\Element\Template;
class Form extends Template
{
protected $_backendUrl;
protected $formKey;
/**
* Form constructor.
* @param Template\Context $context
* @param array $data
* @param FormKey $formKey
*/
public function __construct(
Template\Context $context,
array $data = [],
FormKey $formKey
)
{
$this->formKey = $formKey;
parent::__construct($context, $data);
}
/**
* @return string
*/
public function getFormKey()
{
return $this->formKey->getFormKey();
}
}
Form Codes
<form action="<?php echo $block->getUrl('migration/index/post') ?>" method="post" style="text-align: center;">
<input name="form_key" type="hidden" value="<?php echo $block->getFormKey() ?>">
<label for="sku">Sku: </label>
<input type="text" name="sku" id="sku" size="40">
<input type="submit" value="Submit">
</form>