Third Party Contributions > API Based Plugins

How to call php function from javascript

(1/1)

iBem:
Hi
I am trying to call my php function from javascript file.
This is what I have
php file:

--- Code: ---function init()
{
     $this->include_script('client.js');
     $this->register_action('plugin.test', array($this, 'test'));
}
function test($args)
{
    echo("<script>console.log('PHP: " . "js is working" . "');</script>");
}

--- End code ---
js file:

--- Code: ---rcmail.http_post('plugin.test');

--- End code ---

When I run this I get an error:

jquery.min.js?s=1612812585:36
POST http://localhost/roundcube/?_task=mail&_action=plugin.test 500
(Internal Server Error)

How should this be done correctly and could I also call javascript function from php file ?



SKaero:
The problem is your PHP is invalid, echo isn't a function https://www.php.net/manual/en/function.echo.php

iBem:

--- Quote from: SKaero on June 16, 2021, 12:58:54 AM ---The problem is your PHP is invalid, echo isn't a function https://www.php.net/manual/en/function.echo.php

--- End quote ---

But if I call my test function inside my php file the echo will print text to console. And I get the POST error even if the test function is empty.

SKaero:
Did you post the full plugin PHP file? What is in the Roundcube error log?

Navigation

[0] Message Index

Go to full version