1

How to call javascript function in controller as below

In example.js

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

In exampleController.php

function index(){
   hello();
}
Ashok
  • 184
  • 2
  • 14
  • 2
    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 also try `$this->Html->scriptBlock('alert("hi")');` code to call javaScript function from the view. – Binary Brackets Jun 21 '17 at 06:46
  • Yeah, you are right. Thanks – Ashok Jun 21 '17 at 06:48

1 Answers1

2

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.

Binary Brackets
  • 494
  • 1
  • 4
  • 12