Fork me on GitHub

Calling exported functions and classes from javascript


When a PHP function is registered with Jaxon, the javascript function name is the name of the PHP function prepended with the value of the core.prefix.function configuration option. The default value of this option is jaxon_.

$jaxon->register(Jaxon::CALLABLE_FUNCTION, 'helloWorld');
<button onclick="jaxon_helloWorld(0)">Click Me</button>

When a PHP class is registered with Jaxon, the javascript class name is the name of the PHP class prepended with the value of the core.prefix.class configuration option. The default value of this option is Jaxon.

$jaxon->register(Jaxon::CALLABLE_CLASS, HelloWorld::class);
<button onclick="JaxonHelloWorld.sayHello(0)">Click Me</button>

The parameters of javascript calls

Parameters of various types can be passed to the javascript functions generated by Jaxon: integer, boolean, string, array or object. They are automatically passed to the corresponding PHP classes.

The jaxon.getFormValues(id) function reads the content of the HTML form with a given id.

In this example the content of a HTML form is passed as parameter to the function.

<button onclick="JaxonHelloWorld.sayHello(jaxon.getFormValues('DemoForm'))">Click Me</button>

The jaxon.$(id) function reads the content of the HTML element with a given id.

In this example the value of a text field is passed as parameter to the function.

<button onclick="JaxonHelloWorld.sayHello(jaxon.$('DemoData').value)">Click Me</button>

In this example the value of a checkbox is passed as parameter to the function.

<button onclick="JaxonHelloWorld.sayHello(jaxon.$('DemoData').checked)">Click Me</button>

In this example the content of a HTML element is passed as parameter to the function.

<button onclick="JaxonHelloWorld.sayHello(jaxon.$('DemoDiv').innerHTML)">Click Me</button>

The Request Factory provides functions to generate the above javascript calls as well as their parameters from PHP.