0

I have a controller with custom function getPost(). I want to call this function from my phtml

My phtml code:

<form id="my-form" method="POST" action="<?php echo $block->getUrl('nzrw/vendor/vendorsettingpost/getpost'); ?>">
                <input name="costperitem" class="input-text" type="text" style="width: 100px" /><br />
                <input type="submit" value="Save">
               </form>

My Controller Code:

<?php 

namespace Netzz\ShippingRework\Controller\Vendor;

use Magento\Framework\Controller\ResultFactory; 

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


public function execute() { 

    $post = (array) $this->getRequest()->getPost();
 

    public function getpost() { 

    $post = (array) $this->getRequest()->getPost();
 
        if (!empty($post)) {

            $costperitem   = $post['costperitem'];
            echo $costperitem;
            
            die();
           
        }

    }
}

If I call the execute function, it's working fine, but when I call the getpost function from action, it's not working.

1
  • Share your complete module on git comment git link Commented Nov 3, 2021 at 14:34

3 Answers 3

0

Put your getpost function out side of execute function. Then It should work

0

Re-write the Controller like this and then it will work

<?php

namespace Netzz\ShippingRework\Controller\Vendor;

class Vendorsettingpost extends \Magento\Framework\App\Action\Action
{
    public function execute()
    {
        $this->getpost();
    }
    public function getpost()
    {
        $post = $this->getRequest()->getParams();
        if (!empty($post)) {
            $costperitem   = $post['costperitem'];
            echo $costperitem;
            die();
        }
    }
}
0

Using view_model arguments in block layout.

https://devdocs.magento.com/guides/v2.4/extension-dev-guide/view-models.html

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.