简体   繁体   中英

How to pass value from view to controller without using anchor tag and forms in Zend Framework 2

Used session in my page but expected output not coming for me.i am using ZendSkeletonApplication and ZfcUser

in view/done.phtml

$sessionContainer = new \\Zend\\Session\\Container('myNameSpace');

$checking="Got it";

$sessionContainer->myVar = $checking;

In Controller/UserController.php

public function doneAction()

{
    $result=$this->getRequest()->getPost();
    $sessionContainer = new \Zend\Session\Container('myNameSpace');
    $confirmation=$sessionContainer->myVar;
    print_r($confirmation); 
    return new ViewModel();
}

As far as I know, you cannot send information to the Controller from your view script, since the view(s) are rendered after all the controller->actions are called.

Advice from me would be to not do any logic in your view scripts especially if it would alter the execution of the controller. This would mean that you can place this logic in the controller itself.

The example with the session will not work, because as explained above, the view script executes after the controller, therefore if you set something in the session from the view script, the controller wouldn't "know" about it.

Hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM