Search code examples
cakephpcakephp-3.0cakephp-3.3

How to call javascript function in cakePHP controller?


How to call javascript function in controller as below

In example.js

function hello(){
  alert("hi");
}

In exampleController.php

function index(){
   hello();
}

Solution

  • You can't call JavaScript function from the controller,

    controller is running on server-side, and your java script code running under the browser/client side.

    If you want to call javaScript function you must have to render view and call from that.

    You can also try $this->Html->scriptBlock('alert("hi")'); code to call javaScript function from the view.