AJAX Manufactory
Simplest way to create AJAX application for WordPress
Simplest example
<-- HTML Code (client side) -->
<form class="form form-inline">
<span class="btn btn-info btn-md clickme-btn">Click Me!</span>
</form>
// Javascript (client side)
jQuery(document).ready(function(){
jQuery('.clickme-btn').on('click', function(){
jxAction('click_action1');
});
});
// PHP (server side)
add_action('jx_click_action1', function($jx){
$jx->alert('Hello, world!');
});
A bit more complex example
<-- HTML Code (client side) -->
<form class="form form-inline tryme-form">
<input class="form-control tryme-name" placeholder="Enter your name" name="name" value="">
<span class="btn btn-info btn-md tryme-btn">Try Me!</span>
<br>
<span class="tryme-output label label-info"></span>
</form>
// Javascript (client side)
jQuery(document).ready(function(){
jQuery('.tryme-btn').on('click', function(){
var data = {
'name': jQuery('.tryme-name').val()
};
jxAction('test_action1', data, function(v){
if ('backwards' in v) {
alert("Backwards = " + v.backwards);
}
});
});
});
// PHP (server side)
add_action('jx_test_action1', function($jx){
$name = $jx->data['name'];
if (strlen($name) < 1) {
$jx->alert('Please enter your name');
} else {
$jx->html('.tryme-output', 'Welcome, '.$name.'!');
$jx->variable('backwards', strrev($name));
}
});
Some Facts
Whenever you plan to begin developing applications on WordPress using AJAX technology, you have to solve a number of minor issues unrelated to the logic of your application:
- How to package and transmit data from the browser to the server, so they will not have been distorted?
- How to transfer data, if they are multi-dimensional array?
- How to implement a custom processing of AJAX response in case, again, it represents a complex set of data (a typical example - form sending and on-server validation)?
- How to handle data transition errors?
- How to debug all this stuff?
Usually these issues generates a lot of spaghetti code, moreover, each new request type requires some customization of code. Managing all of this, you even can forget about the main goal of your code!
We have thought of everything and gathered it in one simple but powerful plugin.
Documentation
Content
Client-side functions
There are only two functions which do the thing.
Function | Description |
---|---|
jxAction |
Sends a data using AJAX, receives response and executes sheduled queue from this response. If the callback function specified, jxAction runs it sending "virtual" variables from the server. This function is non-blocking. Returns immediately.jxAction(action, data[, cb])
Result: always returns null (no return). |
jxFormData |
Returns an object containing key=>value pairs of specified form controls data. Useful to simulate usual form submitting. You can use result as the second parameter for jxAction function.
Result: an object containing key => value pairs of data from specified form. |
Server-side methods and properties
You will need to add only one hook action for each "AJAX action".
add_action('jx_<myaction>', function($jx){
// Your AJAX processing code here
});
This inline function will receive an object $jx
of class wpjxmResponse
, which is the main processing class. You will need to use its methods and properties to get request data and make a response.
<my_action>
is the identifier of the AJAX-action which you set up in the client-side function jxAction
.
Properties of object $jx
Property | Description |
---|---|
data |
Returns the data sent from client part with jxAction call.
Example
|
has_priv |
Whether registered user or guest did this jxAction call.
Example
|
Methods of object $jx
Method | Description |
---|---|
alert |
Shows a standard browser's alert window with a message and [OK] button. It uses an original javascript's alert() method.
Example
|
console |
Displays an arbitrary data (could be a string or any-dimensional array) in javascript console. Uses console.log() method in javascript. Very useful for debugging.
Example
|
html |
Put any html data inside node(s) with specified css selector. Uses internal jQuery's method jQuery(selector).html(string) .html(selector, string)
Example
|
redirect |
Make a redirect to specified url. You can also specify a delay (in milliseconds) before redirect actually made. It uses document.location.href = url in javascript.redirect(url[, delay])
Example
|
reload |
Refreshes current page. Nothing special. Uses document.location.reload() in javascript.reload()
Example
|
script |
Executes an arbitrary javascript code using eval() method at the client side.
Example
|
call |
Executes a named function at the client side. Parameter is an array of values to be send to specified function.
Example
|
trigger |
Triggers an event at the client side. The event will be applied to document object. Parameter is an array of values to be send to specified event.
Example
|
variable |
Set up a virtual variable with specified value. Name should be a string, and value can be either boolean, number, string or multidimensional array. At the client side this variable will be passed directly to your callback (in case you are using one in jxAction ).
Example
|
variables |
Can be used to set up lots of virtual variables at the same time. The only parameter is an array of variable names (key) and their respective values.
Example
|
Do you have something to say? Any comments, suggestions and criticisms are welcome!