/* FILE ARCHIVED ON 9:24:52 May 15, 2009 AND RETRIEVED FROM THE AN OPENWAYBACK INSTANCE ON 10:04:32 Jul 10, 2024. JAVASCRIPT APPENDED BY OPENWAYBACK, COPYRIGHT INTERNET ARCHIVE. ALL OTHER CONTENT MAY ALSO BE PROTECTED BY COPYRIGHT (17 U.S.C. SECTION 108(a)(3)). */ /* * AjaxObject is a an object that encapsulates the transaction * request and callback logic. * * handleSuccess( ) provides success case logic * handleFailure( ) provides failure case logic * processResult( ) displays the results of the response from both the * success and failure handlers * startRequest( ) calling this member starts the transaction request. * * Usage Example: * * // The location to the php script that process the JS request * var php_script_location = 'ajax/ajax.php'; * * // Create a new ajax object instance, pass in true to enable error catching * var ajaxObj = new AjaxObject(); * * //Initializes the object to hold the query string formatted arguments * //to be passed to the ajax object for processing * var queryStringArgs = new Object(); * * //Will call the PHP script function getData(). * //Also passes arg1 and arg2 to the PHP script with keys: arg1, arg2, respectively * queryStringArgs['func'] = 'getData'; * queryStringArgs['arg1'] = arg1; * queryStringArgs['arg2'] = arg2; * * //This defines what to do with the resuls of the ajax call to the * //designated script function * ajaxObj.processResult = function (o) * { * //Creates an array of output * var result = o.responseText.split('&'); * alert(result) * } * * //This defines what to do with the resuls of the ajax call to the * //designated script function on failure * ajaxObj.processFailure = function (o) * { * alert('Yahoo connection manager failed.\n' + * 'Click OK to see the results.'); * * var result = ''; * * for (var i in o) * { * result += i + ' => ' + o[i] + '\n'; * } * * alert(result); * } * * //Start the transaction. * ajaxObj.startRequest(php_script_location, queryStringArgs); * */ function AjaxObject(errorReporting) { //Initializes the errorReporting if (errorReporting !== undefined) { this.errorReporting = errorReporting; } else { this.errorReporting = false; } this.handleSuccess = function(o) { //Parse out the error codes from the response var output = o.responseText.split('%$^%ERROR%^$%'); //If there were errors... if (output.length > 1) { var error = output[1]; o.responseText = output[2]; } //If error reporting is turned on... if (this.errorReporting) { if (error !== undefined) { alert(error); } } // This member handles the success response // and passes the response object o to AjaxObject's // processResult member. this.processResult(o); }; this.handleFailure = function(o) { // Failure handler this.processFailure(o); }; this.processResult = function(o) { // This member is called by handleSuccess // THIS METHOD SHOULD BE REDEFINED TO DO WHATEVER IS NEEDED // BY THE JS THAT IS INSTANTIATING THE AjaxObject // Example: AjaxObject.processResult = fuction (o) { alert('hello world'); } }; this.processFailure = function(o) { // This member is called by handleFailure // THIS METHOD SHOULD BE REDEFINED TO DO WHATEVER IS NEEDED // BY THE JS THAT IS INSTANTIATING THE AjaxObject // Example: AjaxObject.processFailure = fuction (o) { alert('failure'); } }; this.startRequest = function(phpScriptLoc, queryStringArgs, method) { /* * Define the callback object for success and failure * handlers as well as object scope. */ var callback = { success: this.handleSuccess, failure: this.handleFailure, scope: this, argument: this }; //Defaults the method to POST if (method === undefined) { method = 'POST'; } var sendStr = this.postEncode(queryStringArgs); YAHOO.util.Connect.asyncRequest(method, phpScriptLoc, callback, sendStr); }; /** * Encodes a variable in a form acceptable to PHP * * @param mixed variable A variable of any type (functions gracefully ignored) * @param string varname Optional variable name; needed for scalars * * @return string The encoded string */ this.postEncode = function(variable, varname) { var enc = ''; var prefix = ''; switch (typeof(variable)) { case 'array': prefix = '_'; // prefix this to the key names // drop through case 'object': if (undefined !== variable.innerHTML) // uh-oh, must be a DOM object { // just store the object's ID enc += prefix + encodeURIComponent(varname) + '=' + encodeURIComponent(variable.id) + '&'; } else { for (var i in variable) { enc += this.postEncode(variable[i], (varname == null) ? i : varname + '[' + i + ']'); } } break; case 'boolean': enc += encodeURIComponent(varname) + '=' + (variable ? '1' : '') + '&'; break; case 'function': break; default: // should we check for missing varname here? enc += encodeURIComponent(varname) + '=' + encodeURIComponent(variable) + '&'; break; } if (varname === null) { enc = enc.substr(0, enc.length - 1); // remove last '&' } return enc; }; }