Create php-js-interoperability.md

This commit is contained in:
Tomas Husak 2021-07-22 10:43:18 +02:00
Родитель 0d4b16cb8c
Коммит 68a99b8bb0
1 изменённых файлов: 33 добавлений и 0 удалений

Просмотреть файл

@ -0,0 +1,33 @@
# PHP JavaScript interoperability
The interoperability is interesting part of the integration. We are able to call JS from PHP and vice versa.
## Calling JavaScript
When we want to call JS function, we just use prepared methods `CallJsVoid` or `CallJs`, which takes the method names, method parameters and calls the desired function for us.
```php
<?php
CallJsVoid("window.alert", "Hello from PHP script.");
?>
```
## Calling PHP
We can also call PHP function, when there is an instance of `PhpScriptProvider` or `PhpComponent`. We just call `window.php.callPHP`, which takes the function name and its parameter as JSON structure.
We can see an example of usage in the example below.
```php
<p>Click and look at console output</p>
<button onclick="window.php.callPHP('CallPHP', { name : 'Bon', surname: 'Jovi'});">PHP</button>
<?php
function CallPHP($data)
{
$json = json_decode($data);
echo "Hello ". $json->name . " " . $json->surname . " from PHP\n";
}
?>
```