irwan

AJAX - PHP Class

Nov 16th, 2011
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.47 KB | None | 0 0
  1. FILE 1 : ajax.js
  2.  
  3. function ajax()
  4. {
  5.    //---------------------
  6.    // Private Declarations
  7.    //---------------------
  8.    var _request = null;
  9.    var _this = null;
  10.        
  11.    //--------------------
  12.    // Public Declarations
  13.    //--------------------
  14.    this.GetResponseXML = function()
  15.    {
  16.       return (_request) ? _request.responseXML : null;
  17.    }
  18.        
  19.    this.GetResponseText = function()
  20.    {
  21.       return (_request) ? _request.responseText : null;
  22.    }
  23.        
  24.    this.GetRequestObject = function()
  25.    {
  26.       return _request;
  27.    }
  28.        
  29.    this.InitializeRequest = function(Method, Uri)
  30.    {
  31.       _InitializeRequest();
  32.       _this = this;
  33.                
  34.       switch (arguments.length)
  35.       {
  36.          case 2:
  37.             _request.open(Method, Uri);
  38.             break;
  39.                                
  40.          case 3:
  41.             _request.open(Method, Uri, arguments[2]);
  42.             break;
  43.       }
  44.                
  45.       if (arguments.length >= 4) _request.open(Method, Uri, arguments[2], arguments[3]);
  46.       this.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
  47.    }
  48.        
  49.    this.SetRequestHeader = function(Field, Value)
  50.    {
  51.       if (_request) _request.setRequestHeader(Field, Value);
  52.    }
  53.        
  54.    this.Commit = function(Data)
  55.    {
  56.       if (_request) _request.send(Data);
  57.    }
  58.        
  59.    this.Close = function()
  60.    {
  61.       if (_request) _request.abort();
  62.    }
  63.        
  64.    //---------------------------
  65.    // Public Event Declarations.
  66.    //---------------------------
  67.    this.OnUninitialize = function() { };
  68.    this.OnLoading = function() { };
  69.    this.OnLoaded = function() { };
  70.    this.OnInteractive = function() { };
  71.    this.OnSuccess = function() { };
  72.    this.OnFailure = function() { };
  73.        
  74.    //---------------------------
  75.    // Private Event Declarations
  76.    //---------------------------
  77.    function _OnUninitialize() { _this.OnUninitialize(); };
  78.    function _OnLoading() { _this.OnLoading(); };
  79.    function _OnLoaded() { _this.OnLoaded(); };
  80.    function _OnInteractive() { _this.OnInteractive(); };
  81.    function _OnSuccess() { _this.OnSuccess(); };
  82.    function _OnFailure() { _this.OnFailure(); };
  83.  
  84.    //------------------
  85.    // Private Functions
  86.    //------------------
  87.    function _InitializeRequest()
  88.    {
  89.       _request = _GetRequest();
  90.       _request.onreadystatechange = _StateHandler;
  91.    }
  92.        
  93.    function _StateHandler()
  94.    {
  95.       switch (_request.readyState)
  96.       {
  97.          case 0:
  98.             window.setTimeout("void(0)", 100);
  99.             _OnUninitialize();
  100.             break;
  101.                                
  102.          case 1:
  103.             window.setTimeout("void(0)", 100);
  104.             _OnLoading();
  105.             break;
  106.                                
  107.          case 2:
  108.             window.setTimeout("void(0)", 100);
  109.             _OnLoaded();
  110.             break;
  111.                        
  112.          case 3:
  113.             window.setTimeout("void(0)", 100);
  114.             _OnInteractive();
  115.             break;
  116.                                
  117.          case 4:
  118.             if (_request.status == 200)
  119.                _OnSuccess();
  120.             else
  121.                _OnFailure();
  122.                                        
  123.             return;
  124.             break;
  125.       }
  126.    }
  127.        
  128.    function _GetRequest()
  129.    {
  130.       var obj;
  131.                
  132.       try
  133.       {
  134.          obj = new XMLHttpRequest();
  135.       }
  136.       catch (error)
  137.       {
  138.          try
  139.          {
  140.             obj = new ActiveXObject("Microsoft.XMLHTTP");
  141.          }
  142.          catch (error)
  143.          {
  144.             return null;
  145.          }
  146.       }
  147.                
  148.       return obj;
  149.    }
  150. }
  151.  
  152.  
  153. FILE 2 : index.php
  154.  
  155. <html>
  156. <head>
  157.  
  158. <script language="Javascript" type="text/javascript">
  159.    someObject = function()
  160.    {
  161.       this.OnSuccess = function()
  162.       {
  163.          document.getElementById('TextDiv').innerHTML = this.GetResponseText();
  164.       }
  165.  
  166.       this.GetData = function()
  167.       {
  168.          this.InitializeRequest('POST', 'testAjax.php');
  169.          this.Commit(null);
  170.       }
  171.    }
  172.    someObject.prototype = new ajax();
  173.  
  174.    myObject = new someObject();
  175. </script>
  176. </head>
  177.  
  178. <body>
  179.    <div id="TextDiv">Text</div>
  180.    <input type="button" value="Click Me" onclick="myObject.GetData();" />
  181. </body>
  182. </html>
  183.  
  184.  
  185.  
  186.  
  187. FILE 3 : testAjax.php
  188.  
  189. <html>
  190.  
  191. <head>
  192.   <title>Hello!</title>
  193. </head>
  194.  
  195. <body>
  196.  
  197. <?php
  198.  
  199. echo ("Hello World!");
  200.  
  201. ?>
  202. </body>
  203.  
  204. </html>
  205.  
  206.  
  207.  
  208.  
  209.  
  210.  
Advertisement
Add Comment
Please, Sign In to add comment