AJAX
[ class tree: AJAX ] [ index: AJAX ] [ all elements ]

Source for file proxy_usage_server.php

Documentation is available at proxy_usage_server.php

  1. <?php
  2. /**
  3.  * Example of Using HTML_AJAX in proxy operation
  4.  *
  5.  * All AJAX calls are handled by the auto_server.php file, server.php could also be used, the differences between the two are covered in those files
  6.  * This is a use case very similar to JPSpan (JPSpan only has a server.php equivalent)
  7.  *
  8.  * The only needed interaction is creation of a new object from the proxy defintion, all AJAX calls happen transparently from there
  9.  *
  10.  * If you want to perform async calls a callback object must be passed to the constructor of the object
  11.  *
  12.  * The client JavaScript library is provided by auto_server.php, you could also copy HTML_AJAX.js (all js files combined) or the seperate js files into your webroot
  13.  * from the PEAR data dir and src them directly.  You can use multiple includes of the component files or use the all flag to get them all at once.
  14.  *
  15.  * @category   HTML
  16.  * @package    AJAX
  17.  * @author     Joshua Eichorn <josh@bluga.net>
  18.  * @copyright  2005 Joshua Eichorn
  19.  * @license    http://www.opensource.org/licenses/lgpl-license.php  LGPL
  20.  * @version    Release: @package_version@
  21.  * @link       http://pear.php.net/package/HTML_AJAX
  22.  */
  23.  
  24. // set a cookie so we always have an example cookie for the test
  25. setcookie('testcookie',"Doesn't taste as good as a peanut butter cookie");
  26.  
  27. ?><html>
  28. <head>
  29.  
  30. <!-- These two calls can be combined into one call if wanted, but its not recomended since it will hurt caching as you might want stubs of multiple classes -->
  31. <script type='text/javascript' src="auto_server.php?client=all"></script>
  32. <!-- Stub is passed the class you want the proxy definition for, you can also use all to get every registered class but that create those auto server
  33.     has to instanciate every class where here only the class used on this page has to be instanciated -->
  34. <script type='text/javascript' src="auto_server.php?stub=test"></script>
  35.  
  36. <script type='text/javascript'>
  37. // definition of the callback javascript class, used to handle async requests
  38. function callback() {}
  39. callback.prototype = {
  40.     echo_string: function(result) {
  41.         document.getElementById('target').innerHTML = result;
  42.     },
  43.     cookies: function(result) {
  44.         var ret = "";
  45.         for(var i in result) {
  46.             ret += i+':'+result[i]+"\n";
  47.         }
  48.         document.getElementById('target').innerHTML = ret;
  49.     },
  50.     echo_data: function(result) {
  51.         document.getElementById('target').innerHTML = HTML_AJAX_Util.varDump(result);
  52.     },
  53.     unicode_data: function(result) {
  54.         document.getElementById('target').innerHTML = HTML_AJAX_Util.varDump(result);
  55.     },
  56.     dump: function(result) {
  57.         document.getElementById('target').innerHTML = result;
  58.     }
  59. }
  60.  
  61. // function used to clear out the target div
  62. function clearTarget() {
  63.     document.getElementById('target').innerHTML = 'clear';
  64. }
  65. </script>
  66. </head>
  67. <body>
  68. <script type="text/javascript">
  69. // create a proxy in sync mode
  70. var syncProxy = new test();
  71. // create a proxy in async mode
  72. var asyncProxy = new test(new callback());
  73.  
  74. // run a sync call and set its results to the target div
  75. function syncCall() {
  76.     document.getElementById('target').innerHTML = syncProxy.echo_string("I'm a sync call");
  77. }
  78.  
  79. // run a sync call, callback class will handle its results
  80. function asyncCall() {
  81.     asyncProxy.echo_string("I'm a async call");
  82. }
  83.  
  84. function unicodeTest() {
  85.     //asyncProxy.echo_data({'suggestion': ['Français', 'caractères']});
  86.     asyncProxy.echo_data({"suggestion":["Fran\u00e7ais","caract\u00e8res"]});
  87. }
  88.  
  89. function unicodeTest2() {
  90.     asyncProxy.unicode_data();
  91. }
  92.  
  93. function cookieTest() {
  94.     asyncProxy.cookies();
  95. }
  96. function assocTest() {
  97.     asyncProxy.dump({a: 'first var', b: 'second var'});
  98. }
  99. </script>
  100. <ul>
  101.     <li><a href="javascript:clearTarget()">Clear Target</a></li>
  102.     <li><a href="javascript:syncCall()">Run Sync Echo call</a></li>
  103.     <li><a href="javascript:asyncCall();">Run Async Echo call</a></li>
  104.     <li><a href="javascript:unicodeTest();">Check ability to round trip utf-8 JSON data</a></li>
  105.     <li><a href="javascript:unicodeTest2();">Check ability to recieve utf-8 JSON data</a></li>
  106.     <li><a href="javascript:assocTest();">Check ability to decode js hashes into PHP associative arrays using JSON</a></li>
  107.     <li><a href="javascript:cookieTest();">View Cookies</a></li>
  108. </ul>
  109.  
  110. <div style="white-space: pre; padding: 1em; margin: 1em; width: 600px; height: 300px; border: solid 2px black; overflow: auto;" id="target">Target</div>
  111.  
  112. <div>
  113. Runing view Cookies should show you these same cookies being returned by the AJAX call<br>
  114. If the lists don't match, try reloading this page, it sets a test cookie, but we can't see it on the PHP side until the client returns it on a test.
  115. <pre>
  116. <?php
  117.     var_dump($_COOKIE);
  118. ?>
  119. </pre>
  120. </div>
  121.  
  122. </body>
  123. </html>

Documentation generated on Sat, 05 May 2007 18:00:20 -0400 by phpDocumentor 1.3.0. PEAR Logo Copyright © PHP Group 2004.