how to pass variable from View To Controller Action In Zend Framework

3.6k views Asked by At

In my view i have this line of code

<a href="<?php echo $this->url.'/login/calllogs/id/'.$record->phone_service_id;?>">Control Panel</a>

in my Login Controller I have this action

public function calllogsAction(){
    if(!Zend_Auth::getInstance()->hasIdentity()){
        $this->_redirect('login/login');
    } else{
        $request = $this->getRequest();
        $this->view->assign('url', $request->getBaseURL());
    }
}

How can i get my view variable ($record->phone_service_id;) in my action (calllogsAction)? I am not submitting , this is just a link. I think i cant do it it like this in my action calllogsAction

$request = $this->getRequest();
$phone_service_id =  $request->getParam("id");
2

There are 2 answers

4
bububaba On BEST ANSWER

If you don't want the id to show in the address bar, then you will have to use POST. Use

<form method="post" action="<?php /*your URL here*/ ?>">

instead of a plain link, inside the form:

<input type="hidden" name="id" value="<?php /*your id here*/ ?>"/>

and in the controller use

$id = $this->getRequest()->getPost("id");
4
Hecksa On

Initial answer was the same as your attempt, as it turns out.

If you don't want the id in the address bar (bear in mind anyone can see post data if they have the right tools anyway) then your other option is using POST with a form -

<form action="<?php echo $this->url.'/login/calllogs/id';?>" method="post">
<input type="hidden" name="id" value="<?php echo $record->phone_service_id; ?>" />
<input type="submit" name="submit" value="Control Panel" />
</form>

You'd then need to use getPost rather than getParam in the controller to get the variable.