// Global variables var server_file_uri = "/cc/async_php.php"; // What to call. (PHP to Javascript.) var async_direct = 0; // Write directly to the element ///////// API functions ///////// // runs a function with a param[s] returns to a callback function function async_call(js_callback_func, php_file_uri, php_function_name, php_params) { var name = (js_callback_func == '' || js_callback_func == undefined) ? "" : "&c=" + js_callback_func; var argsr = Array.prototype.slice.call(arguments); args = js2php(argsr.slice(3), 'p'); async_exec(name, encodeURIComponent(php_file_uri), php_function_name, args); } // runs a function with a param[s] and array returns to a callback function function async_call_array(js_callback_func, php_file_uri, php_function_name, php_params, aa) { var name = (js_callback_func == '' || js_callback_func == undefined) ? "" : "&c=" + js_callback_func; var argsr = Array.prototype.slice.call(arguments); args = php_params; async_exec(name, encodeURIComponent(php_file_uri), php_function_name, args, aa); } // runs a function with a param[s] with write to an id'ed element // used because there are times MSIE can not just write to the element // (inside forms, I think), but it messes up HTML tags // -- converts them for viewing function async_write(id, php_file_uri, php_function_name, params, p2) { var name = (id == '' || id == undefined) ? "" : "&i=" + id; var argsr = Array.prototype.slice.call(arguments); args = js2php(argsr.slice(3),'p'); async_exec(name, encodeURIComponent(php_file_uri), php_function_name, args); } // runs a function with a param[s] with direct write to an id'ed element // used when HTML tags are in the response. Be careful, there are places // MSIE can not do this. (In forms, I think.) function async_write_direct(id, php_file_uri, php_function_name, php_params) { async_direct = 1; var name = (id == '' || id == undefined) ? "" : "&i=" + id; var argsr = Array.prototype.slice.call(arguments); args = js2php(argsr.slice(3),'p'); async_exec(name, encodeURIComponent(php_file_uri), php_function_name, args); } //////// Support functions ///////// function async_exec(name, php_file_uri, php_function_name, php_parms, aa) { // Uses AJAX to call the php and display the results var dir = (async_direct == 1) ? "&d=1" : ""; var pa = (typeof(aa) == 'object') ? "&ka=1&" + js2php(aa,'a') : ""; if (typeof(aa) == 'object') php_parms = "p=" + php_parms; // Create new JS element var jsel = document.createElement('SCRIPT'); jsel.setAttribute("type","text/javascript"); jsel.setAttribute("id","ajaxSelect"); jsel.setAttribute("src", server_file_uri + '?' + 'u=' + encodeURI(php_file_uri) + '&f=' + php_function_name + '&' + php_parms + name + dir + pa); // Append JS element (therefore executing the 'AJAX' call for the reply) // It will return and run script var sp2 = document.body.firstChild; document.body.insertBefore(jsel, sp2); } // From: http://www.php.net/manual/en/function.json-decode.php // paul at sfdio dot com 21-Jan-2007 03:08 // Modified for p[]. Jeff Hennick 8/25/10 // Modified for name param. Jeff Hennick 3/22/2011 function js2php(obj,name,path,new_path) { if (typeof(path) == 'undefined') var path=[]; if (typeof(new_path) != 'undefined') path.push(new_path); var post_str = []; if (typeof(obj) == 'array' || typeof(obj) == 'object') { for (var n in obj) { post_str.push(js2php(obj[n],name,path,n)); } } else if (typeof(obj) != 'function') { var base = path.shift(); post_str.push(name + '[' + base + (path.length > 0 ? '[' + path.join('][') + ']' : '') + ']=' + encodeURI(obj)); path.unshift(base); } path.pop(); return post_str.join('&'); }