Skip to content

using AJAX framework

minhaz edited this page Jun 27, 2015 · 1 revision

##Backend Every request has two parts category and data. category is unique for each request and data is the payload.

In the ajaxserver\index.php for each ajax request add an entry in $access_needed array with array of access needed to perform the request. Also for each request you should have a file named ajaxserver\<category>.php where the logic for the request is handled. Those scripts (<category>.php) should make changes to $output variable which is returned back in ajax request.

By default it looks like:

$output = array(
	'error' => false,
	'message' => '',
	'logout' => false,
	'data' => null
	);

set error to true if some error has occurred. The js code will pick it up and show notification. message data is shown in notification as the request completes. All data you need to send back to js client should be added to data as an array, string or int as need be. It can be handled in js code.

##Frontend In frontend include the js/main.js code. The js constructor looks like:

var AJAX = function(category, data, successCallback, failureCallback)

so you need to call method like (for example delete admin)

new AJAX('delete_admin', {admin_id: 11}, function(d) {
    $("#ADMIN_LIST tr[data-id='" +d.data +"']").remove();
}, function(d) {
    $("#ADMIN_LIST tr[data-id='" +d.data +"']").css('color', 'red');
    alert('unable to remove admin');
});
Clone this wiki locally