0

I am trying to use form POST method in my custom backend module from phtml, reloading the same page. But I get a 404. I have added form key. Been at it for days, so would appreciate some help.

Company/Logistikk/view/adminhtml/templates/handleliste.phtml :

<?php 
    $action = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
    if(isset($_POST['test_string'])){
        $testString = $_POST['test_string'];
        echo $testString;
    }
?>
<form name="test_form" method="POST" id="test-form" autocomplete="off">
  <input name="form_key" type="hidden" value="<?php /* @noEscape */ echo $block->getFormKey();?>" />
    <div class="admin__form-field-control">
        <input type="text" name="test_string" placeholder="Write something..." class="admin__control-text" action"<?php echo $action; ?>" />
    </div>
    <div class="admin__footer-main-actions">
        <input type="submit"  class="action-secondary" value="Send" />
    </div>
</form>

Company/Logistikk/Block/Logistikk.php :

<?php
namespace Company\Logistikk\Block;

use Magento\Backend\Block\Widget\Context;
use Magento\Framework\Data\Form\FormKey;
use Magento\Framework\View\Element\Template;
class Logistikk extends Template
{
    protected $formKey;
    public function __construct(Context $context, FormKey $formKey, array $data = [])
    {
        $this->formKey = $formKey;
        parent::__construct($context, $data);
    }
    public function getFormKey()
    {
        return $this->formKey->getFormKey();
    }
}
?>

Company/Logistikk/view/adminhtml/layout/logistikk_handleliste_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">
    <referenceBlock name="page.title">
        <action method="setPageTitle">
            <argument name="title" xsi:type="string">Company Handleliste</argument>
        </action>
    </referenceBlock>
    <body>
        <referenceContainer name="content">
            <block class="Company\Logistikk\Block\Logistikk" template="Company_Logistikk::handleliste.phtml"/>
        </referenceContainer>
    </body>
</page>
7
  • Do you have associated adminhtml layout file? Where do you need your block to show?
    – dudzio
    Commented Jun 1, 2021 at 18:35
  • I have added the layout file now. I just want to reload the same page, and read the POST data. As I understand leaving the action= out will reload the same page, but I get a 404. I'm rebuilding a module I made in M1 for M2, but I must be missing something..
    – Dandy
    Commented Jun 1, 2021 at 18:50
  • I'll debug your code on my test instance, give me a minute ;)
    – dudzio
    Commented Jun 1, 2021 at 18:54
  • Thanks, and thanks for the company name edit as well ;)
    – Dandy
    Commented Jun 1, 2021 at 18:55
  • Have you tested it set to use GET? This might eliminate form_key/CSRF as the culprit.
    – jiheison
    Commented Jun 1, 2021 at 18:58

1 Answer 1

0

Place all your adminhtml area related templates inside view/adminhtml/templates directory:

Company/Logistikk/view/adminhtml/templates/handleliste.phtml

You can easy debug your block appearance by adding them on all pages, so create new layout file view/adminhtml/layout/default.xml with content:

<?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 name="handleliste" class="Company\Logistikk\Block\Logistikk" template="Company_Logistikk::handleliste.phtml"/>
        </referenceContainer>
    </body>
</page>

You didn't had name attribute in your custom block, so it was producing exception when layout was loading.

If that works, the next step is to add your layout changes in your action specific layout file logistikk_handleliste_index.xml. If it won't show - search for issues inside controller action classes then.

6
  • I forgot the /templates part when I wrote the post, it was in the right place on the server. I did as suggested, and without the default.xml I still get the 404. With the default.xml I get a working result, but with an added 404 error under the block ;)
    – Dandy
    Commented Jun 1, 2021 at 19:37
  • Great news, I suggest to work on developer mode instance, so you can see the errors on the screen and not only in log files ;)
    – dudzio
    Commented Jun 1, 2021 at 19:43
  • Thanks a lot for your help @dudzio. I still can't get it to work with POST method without using default.xml, so because I'm pressed for time, I think I will use GET. Anyway, I found this in the log:
    – Dandy
    Commented Jun 1, 2021 at 20:58
  • main.DEBUG: URI '/admin/logistikk/handleliste/index/key/71e303af0bf28e483b70ed3792b15413bcf66577a40fdecab2c0b403e47a6e9b/'' cannot be accessed with POST method (Company\Logistikk\Controller\Adminhtml\Handleliste\Index) [] [] [2021-06-01 20:44:05] main.DEBUG: Request validation failed for action "Company\Logistikk\Controller\Adminhtml\Handleliste\Index\Interceptor" {"exception":"[object] (Magento\\Framework\\App\\Request\\InvalidRequestException(code: 0): Invalid request received at /var/www/html/magento2/vendor/magento/framework/App/Request/HttpMethodValidator.php:69)
    – Dandy
    Commented Jun 1, 2021 at 20:58
  • I'm not sure what that means, but you definitely should not use $_POST as is. You should get your post data from Block class with $this->getRequest() instead ;)
    – dudzio
    Commented Jun 1, 2021 at 21:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.