How to call javascript function in cakePHP controller?

1.8k views Asked by At

How to call javascript function in controller as below

In example.js

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

In exampleController.php

function index(){
   hello();
}
1

There are 1 answers

0
Binary Brackets On BEST ANSWER

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.