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

Source for file proxyless_usage.php

Documentation is available at proxyless_usage.php

  1. <?php
  2. /**
  3.  * Example of Using HTML_AJAX in proxyless operation
  4.  *
  5.  * This is the simplest way to use HTML_AJAX if your just using grab and replace functions you don't even need a server
  6.  * You need every javascript file except JSON which is optional and is only needed if your using that encoding
  7.  *
  8.  * The proxyless api is provided by Main.js
  9.  *
  10.  * There are 4 main methods and 2 properties to the proxyless api, they all exist as static methods on HTML_AJAX
  11.  *    HTML_AJAX.grab(url)
  12.  *    HTML_AJAX.post(url,payload)
  13.  *    HTML_AJAX.replace(id,url) or HTML_AJAX.replace(id,class,method,arg1,arg2,etc)
  14.  *    HTML_AJAX.call(class,method,callback,arg1,arg2,etc)
  15.  *
  16.  *    HTML_AJAX.defaultServerUrl = 'serverurl';
  17.  *    HTML_AJAX.defaultEncoding = 'Null';
  18.  *
  19.  * The api is demonstrated below, server.php is used for call examples and to load the needed js files
  20.  *
  21.  * @category   HTML
  22.  * @package    AJAX
  23.  * @author     Joshua Eichorn <josh@bluga.net>
  24.  * @copyright  2005 Joshua Eichorn
  25.  * @license    http://www.opensource.org/licenses/lgpl-license.php  LGPL
  26.  * @version    Release: 0.5.4
  27.  * @link       http://pear.php.net/package/HTML_AJAX
  28.  */
  29. ?>
  30. <html>
  31. <head>
  32.  
  33. <script type='text/javascript' src="server.php?client=Util"></script>
  34. <script type='text/javascript' src="server.php?client=main"></script>
  35. <script type='text/javascript' src="server.php?client=dispatcher"></script>
  36. <script type='text/javascript' src="server.php?client=HttpClient"></script>
  37. <script type='text/javascript' src="server.php?client=Request"></script>
  38. <script type='text/javascript' src="server.php?client=json"></script>
  39. <script type='text/javascript' src="server.php?client=loading"></script>
  40. <script type='text/javascript' src="server.php?client=iframe"></script>
  41. <script type='text/javascript' src="server.php?client=urlserializer"></script>
  42.  
  43. </head>
  44. <body>
  45. <script type="text/javascript">
  46. function clearTarget() {
  47.     document.getElementById('target').innerHTML = 'clear';
  48. }
  49.  
  50. // Add an error handler so we get an alert if any errors happen while making an AJAX call
  51. HTML_AJAX.onError = function(e) 
  52. {
  53.     alert(HTML_AJAX_Util.varDump(e));
  54. }
  55.  
  56.  
  57. // Grab is the simplest usage of HTML_AJAX you use it to perform a request to a page and get its results back
  58. // It can be used in either Sync mode where it returns directory or with a call back, both methods are shown below
  59. var url = 'README';
  60. function grabSync() {
  61.     document.getElementById('target').innerHTML = HTML_AJAX.grab(url);
  62. }
  63.  
  64. function grabAsync() {
  65.     HTML_AJAX.grab(url,grabCallback);
  66. }
  67.  
  68. function grabCallback(result) {
  69.     document.getElementById('target').innerHTML = result;
  70. }
  71.  
  72. // POST lets you make simple POST requests to a url, if you use a hash {key:value,key2:value,etc} as the payload then the
  73. // data will be automatically be urlencoded, making it look like a form submission to the server
  74. // if the data is a string it will be sent as is
  75. var postUrl = 'support/post_tester.php';
  76. function postSync() {
  77.     document.getElementById('target').innerHTML = HTML_AJAX.post(postUrl,{dog:'bob',cat:'tigger'});
  78. }
  79.  
  80. function postAsync() {
  81.     HTML_AJAX.post(postUrl,{dog:'bob',cat:'tigger'},grabCallback);
  82. }
  83.  
  84. function postCallback(result) {
  85.     document.getElementById('target').innerHTML = result;
  86. }
  87.  
  88.  
  89. // replace can operate either against a url like grab or against a remote method
  90. // if its going to be used against a remote method defaultServerUrl needs to be set to a url that is exporting the class its trying to call
  91. // note that replace replace always works async
  92.  
  93. HTML_AJAX.defaultServerUrl = 'server.php';
  94.  
  95. function replaceUrl() {
  96.     HTML_AJAX.replace('target',url);
  97. }
  98.  
  99. function replaceFromMethod() {
  100.     HTML_AJAX.replace('target','test','echo_string','Im a method call replacement');
  101. }
  102. function replaceFromMethodMulti() {
  103.     // Null encoding does not support multiple arguments, you have to use JSON for that
  104.     HTML_AJAX.defaultEncoding = 'JSON'; // set encoding to JSON encoding method
  105.     HTML_AJAX.replace('target','test','multiarg','argument1','argument2','argument3');
  106.     HTML_AJAX.defaultEncoding = 'Null'; // return it to default which is Null
  107. }
  108.  
  109.  
  110. // call is used to call a method on a remote server
  111. // you need to set HTML_AJAX.defaultServerUrl to use it
  112. // you might also want to set HTML_AJAX.defaultEncoding, options are Null and JSON, the server will autodetect this encoding from your content type
  113. // but the return content type will be based on whatever the servers settings are
  114. // You can use call in either Sync or Async mode depending on if you pass it a callback function
  115.  
  116. function callSync() {
  117.     HTML_AJAX.defaultEncoding = 'JSON'; // set encoding to JSON encoding method
  118.     document.getElementById('target').innerHTML = HTML_AJAX.call('test','echo_string',false,'Im text that was echoed');
  119.     HTML_AJAX.defaultEncoding = 'Null'; // return it to default which is Null
  120. }
  121.  
  122. function callAsync() {
  123.     HTML_AJAX.call('test','echo_string',callCallback,'Im text that was echoed Async');
  124. }
  125.  
  126. function callCallback(result) {
  127.     document.getElementById('target').innerHTML = result;
  128. }
  129.  
  130. </script>
  131. <ul>
  132.     <li><a href="javascript:clearTarget()">Clear Target</a></li>
  133.     <li><a href="javascript:grabSync()">Run Sync Grab Example</a></li>
  134.     <li><a href="javascript:grabAsync()">Run Async Grab  Example</a></li>
  135.     <li><a href="javascript:postSync()">Run Sync Post Example</a></li>
  136.     <li><a href="javascript:postAsync()">Run Async Post  Example</a></li>
  137.     <li><a href="javascript:replaceUrl()">Replace with content from a url</a></li>
  138.     <li><a href="javascript:replaceFromMethod()">Replace with content from a method call</a></li>
  139.     <li><a href="javascript:replaceFromMethodMulti()">Replace with content from a method call (multiple method arguments)</a></li>
  140.     <li><a href="javascript:callSync()">Sync Call</a></li>
  141.     <li><a href="javascript:callAsync()">ASync Call</a></li>
  142. </ul>
  143.  
  144. <div style="white-space: pre; padding: 1em; margin: 1em; width: 600px; height: 300px; border: solid 2px black; overflow: auto;" id="target">Target</div>
  145. </body>
  146. </html>
  147. <?php 
  148. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  149. ?>

Documentation generated on Fri, 04 Apr 2008 18:30:23 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.