Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP Send query with link

Tags:

php

cakephp

I have created a link on my app to allow users to create appointments, but need to pass some extra data across so the app knows which client to book the appointment to. How would I do this?

e.g. <li><?php echo $this->Html->link('Book Appointment', array('admin' => true, 'controller' => 'appointments', 'action' => 'add')); ?></li>

So the url would be something like: /admin/appointments/add?clientid=2

NOTE: If someone thinks this is a bad way to do this please also comment with alternate solutions etc. Thanks

like image 998
Cameron Avatar asked Jul 16 '11 11:07

Cameron


2 Answers

I found this in the documentation: http://book.cakephp.org/view/1442/link

You can do the following:

<li><?php echo $this->Html->link('Book Appointment', array(
    'admin' => true,
    'controller' => 'appointments',
    'action' => 'add',
    '?' => array('clientid' => $clientId))
); ?></li>

The code is untested since I don't have CakePHP installed and there was a long time since I used it. I'm sure you can get the example working by looking at the documentation aswell.

like image 193
Tobias Avatar answered Nov 01 '22 07:11

Tobias


You can transfer the id either as a named or unnamed parameter.

Unnamed parameter (the easier way), assuming the id is in $clientId:

$this->Html->link('Book Appointment', array('admin' => true, 'controller' => 'appointments', 'action' => 'add', $clientId )) and the url looks like /admin/appointments/add/2

In the controller: function add( $clientId ) { ... }

Named parameter:

$this->Html->link('Book Appointment', array('admin' => true, 'controller' => 'appointments', 'action' => 'add', 'clientId' => $clientId )) and the url looks like /admin/appointments/add/clientId:2

In the controller: function add() { $clientId = $this->params[ 'named' ][ 'clientId' ]; ... }

like image 26
JJJ Avatar answered Nov 01 '22 06:11

JJJ