Author Topic: How to call php function from javascript  (Read 5138 times)

Offline iBem

  • Newbie
  • *
  • Posts: 4
How to call php function from javascript
« 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 ?




Offline SKaero

  • Administrator
  • Hero Member
  • *****
  • Posts: 5,876
    • SKaero - Custom Roundcube development
Re: How to call php function from javascript
« Reply #1 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

Offline iBem

  • Newbie
  • *
  • Posts: 4
Re: How to call php function from javascript
« Reply #2 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.

Offline SKaero

  • Administrator
  • Hero Member
  • *****
  • Posts: 5,876
    • SKaero - Custom Roundcube development
Re: How to call php function from javascript
« Reply #3 on: June 16, 2021, 10:54:13 AM »
Did you post the full plugin PHP file? What is in the Roundcube error log?