Sometimes, in CakePHP I've needed to use a javascript function whose code was the code from a function I had created previously in one of my controller. Specifically when I wanted to show a list of users with a link for each user to modify their profile in a "flash element" of CakePHP. Therefore, and as I find it very useful, I'm going to specify the steps to do this.
1 .- Having created the function we'll use in the controller. For example:
function modifica_alumno($id_alumno){ ...}2 .- We use for example,
scroll.js (we could use any javascript file but in this case I've chosen this), in folder app / webroot / js, we edit it and introduce something like:
function perfil_alumno(id_alumno){ var aDIV = "left-center"; var aURL = "/alumnos/modifica_alumno/"+id_alumno; new Ajax.Updater(aDIV,aURL, {method:'post', asynchronous:true, evalScripts:true}); }3 .- In another controller's function we refer to the JavaScript function we've just created and display the results by a "flash."
function buscar_alumno($nombre){ ...
foreach($consulta as $sol){ $listado .='<a style="text-decoration:none; color:green;" href="#" onclick="perfil_alumno('.$id_alumno.');return false;"> - '.$alumno['nombre'].' '.$alumno['apellido1'].' '.$alumno['apellido2'].'</a><br/><br/>'; } $this->flash($listado,'index');}Here we choose where we want the function to be load in the div, we refer to the controller's function we want to use and ready.
Greetings!!