1

I'm trying go get parameters from my submitted form (my form is devided in two parts). When I submit my first form, my page go to the second part (on the right view). But I'm stuck here. I have that :

class Form extends Action
{
    protected $_resultPageFactory;

    public function __construct(Context $context, PageFactory $resultPageFactory)
    {
        $this->_resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }


    public function execute()
    {
        $name = $this->getRequest()->getParam('name');
        $resultPage = $this->_resultPageFactory->create();
        return $resultPage;
    }

How do I display $name on my view ? Here's my view, I tried :

<?php echo $name; ?>
<?php echo $this->name; ?>

None of these worked.

EDIT : in post method in my form, my url is like that :

http://dev.site.com/connexion/connexion/Form/name/nameofclient/code/00theCode00/

EDIT SOLUTION : This one didn't work :

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

But this one did :

echo $block->getRequest()->getParam('name');

Thank you very much for your answer, it was helpful !

  • You can directly call inside phtml file like below, $params = $block->getRequest()->getParams(); – Haritha May 23 '18 at 16:57
  • I tried this : <?php $params = $block->getRequest()->getParams(); var_dump($params);?> but it's not working either – Morgan Tartreau May 24 '18 at 7:53
  • @MorganTartreau do you got solution? – kunj May 26 '18 at 20:02
  • Yes, see "edit solution" in my first post – Morgan Tartreau May 28 '18 at 13:04
0

Write below code in Controller

protected $_name;
protected $_resultPageFactory;


public function __construct(Context $context, PageFactory $resultPageFactory)
{
    $this->_resultPageFactory = $resultPageFactory;
    parent::__construct($context);
}

public function execute()
{
    $this->getVariable(); 
    $resultPage = $this->_resultPageFactory->create();
    return $resultPage;
}

public function getVariable()
{
   $this->_name = $this->getRequest()->getParam('name'); 
   return $this->_name;
}

Call "getVariable" function in view page.

Note: view.phtml file will be this block view file.

$block->getVariable();

  • Thanks but it's not working, I have a complete white page. I need to had ($resultPage = $this->_resultPageFactory->create(); return $resultPage;) otherwise my page is empty, i don't know why (i'm new in magento) – Morgan Tartreau May 23 '18 at 15:36
  • are you in get request or post request? – MR.R DNATH May 23 '18 at 15:49
  • try updated code – MR.R DNATH May 23 '18 at 15:52
  • I'm in post, but my input value are in the url (i don't know why, that's strange...) – Morgan Tartreau May 23 '18 at 15:59
  • I have no error, but the name is not display – Morgan Tartreau May 23 '18 at 16:00
0

Try this:

class Form extends Action
{
    protected $_resultPageFactory;
    protected $_helper;
    public function __construct(
        Context $context, 
        PageFactory $resultPageFactory,
        {{Your module package}}\{{Your module }}\Helper\Data $helper
    )
    {
        $this->_helper = $helper;
        $this->_resultPageFactory = $resultPageFactory;
        parent::__construct($context);
    }


    public function execute()
    {
        $name = $this->getRequest()->getParam('name');
        $this->_helper->setName($name);
        $resultPage = $this->_resultPageFactory->create();
        return $resultPage;
    }

Create your helper file and set data in that object.

class Data extends \Magento\Framework\App\Helper\AbstractHelper
{
    protected $_setFormName;

    public function setName($formName)
    {
        $this->_setFormName = $formName;
        return $this;
    }

    public function getName()
    {
        return $this->_setFormName;
    }

}

Now you can access Helper in your PHTML with this.

$_helper = $this->helper('{{Your module package}}\{{Your module }}\Helper\Data');
$_helper->getName();
  • Where do I put the helper file ? And what is it ? I read that it's some code I can use anywhere in my website, but i only need it to be in only one part of the site. Will there be any security issue with that ? Thx – Morgan Tartreau May 24 '18 at 7:47
  • Sorry, It was my mistake, I have updated my answer. You need to create new helper file. You can put this code: $_helper = $this->helper('{{Your module package}}\{{Your module }}\Helper\Data'); $_helper->getName(); In your PHTML file. – kunj May 24 '18 at 8:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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