Roundcube Community Forum

Third Party Contributions => API Based Plugins => Topic started by: iBem on June 15, 2021, 06:46:33 PM

Title: How to call php function from javascript
Post by: iBem on June 15, 2021, 06:46:33 PM
Hi
I am trying to call my php function from javascript file.
This is what I have
php file:
Code: [Select]
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>");
}
js file:
Code: [Select]
rcmail.http_post('plugin.test');

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 ?



Title: Re: How to call php function from javascript
Post by: 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
Title: Re: How to call php function from javascript
Post by: iBem on June 16, 2021, 01:33:19 AM
The problem is your PHP is invalid, echo isn't a function https://www.php.net/manual/en/function.echo.php

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.
Title: Re: How to call php function from javascript
Post by: SKaero on June 16, 2021, 10:54:13 AM
Did you post the full plugin PHP file? What is in the Roundcube error log?