sonoton345 Posted June 23, 2009 Share Posted June 23, 2009 How can I pass a url parameter from a view to a controller. I have a link that's like this in my view... <a href="http://localhost/jobsite/jobseekers/companyjobs/<?php echo $result_new['employers']['id']; ?>">View jobs by this company</a> I would like to pass the value of $result_new['employers']['id'] to a function in my controller to use in running a sql query. Is this possible and if yes how can I do this? Many thanks. Quote Link to comment https://forums.phpfreaks.com/topic/163438-passing-url-parameter-to-a-controller-in-cakephp/ Share on other sites More sharing options...
jcombs_31 Posted June 23, 2009 Share Posted June 23, 2009 This should be pretty easy. Just pass the variable like so function test($id) { if($id) { // do something with $id } } in the view you should use the html helper echo $html->link('View jobs by this company', '/controller/test/' . $result_new['employees']['id']); Quote Link to comment https://forums.phpfreaks.com/topic/163438-passing-url-parameter-to-a-controller-in-cakephp/#findComment-862348 Share on other sites More sharing options...
sonoton345 Posted June 24, 2009 Author Share Posted June 24, 2009 I actually want to pass from view to controller, not from controller to view. I want to pass the value of $result['employer']['id'] to the controller. I tried ur code earlier on while struggling with but it's not sending any value to the controller. function companyjobs($id){ if($this->Session->check('userid')) { if($this->Session->read('usertype')=='jobseeker') { $id = $this->params['Employer']['id']; if($this->data['Employer']['id']==' ') { $this->redirect(array('action'=>'companymatch1')); } else { $result = $this->Postjob->query("SELECT postjobs.* FROM employers,postjobs WHERE postjobs.employer_id=employers.id AND employers.id='".$id."' "); } if(!empty($result)) { $this->set('result',$result); } else { $this->redirect(array('action'=>'companymatch')); } } if($this->Session->read('usertype')=='employer') { $this->Session->setFlash('You are already logged in as an employer.', true); $this->redirect(array('controller'=>'employers','action'=>'welcome/')); } } else { $this->Session->setFlash('You are not logged in or your session has expired. Please login again.', true); $this->redirect(array('action'=>'index/')); } $this->layout = 'mytemp2'; } Quote Link to comment https://forums.phpfreaks.com/topic/163438-passing-url-parameter-to-a-controller-in-cakephp/#findComment-862448 Share on other sites More sharing options...
jcombs_31 Posted June 24, 2009 Share Posted June 24, 2009 What I posted is doing exactly what you asked. It is passing the value of $id from the view to the controller. You are not checking for $id in your controller logic. Quote Link to comment https://forums.phpfreaks.com/topic/163438-passing-url-parameter-to-a-controller-in-cakephp/#findComment-862744 Share on other sites More sharing options...
sonoton345 Posted June 24, 2009 Author Share Posted June 24, 2009 Thanks. Just did it another way, sent the value of $id as an hidden field to the controller ... Quote Link to comment https://forums.phpfreaks.com/topic/163438-passing-url-parameter-to-a-controller-in-cakephp/#findComment-862821 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.