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

Source for file xml_usage.php

Documentation is available at xml_usage.php

  1. <?php
  2. /**
  3.  * Example of Using HTML_AJAX in proxy operation
  4.  *
  5.  * All AJAX calls are handled by the xmlserver.php
  6.  *
  7.  * The only needed interaction is creation of a new object from the proxy defintion, all AJAX calls happen transparently from there
  8.  *
  9.  * If you want to perform async calls a callback object must be passed to the constructor of the object
  10.  *
  11.  * @category   HTML
  12.  * @package    AJAX
  13.  * @author     Elizabeth Smith <auroraeosrose@gmail.com>
  14.  * @copyright  2006 Elizabeth Smith
  15.  * @license    http://www.opensource.org/licenses/lgpl-license.php  LGPL
  16.  * @version    Release: 0.5.4
  17.  * @link       http://pear.php.net/package/HTML_AJAX
  18.  */
  19.  
  20. ?><html>
  21. <head>
  22.  
  23. <script type='text/javascript' src="xmlserver.php?client=all"></script>
  24. <script type='text/javascript' src="xmlserver.php?stub=testxml"></script>
  25.  
  26. <script type='text/javascript'>
  27.  
  28. // function to display xml received from server
  29. function showItems(xml)
  30. {
  31.     var list=xml.getElementsByTagName('item');
  32.     document.getElementById('target').innerHTML = '<p>My Fridge</p>';
  33.     for (var i=0;i<list.length;i++)
  34.     {
  35.         var node = list[i];
  36.         document.getElementById('target').innerHTML += '<p>' + node.firstChild.nodeValue
  37.             + ' is a ' + node.getAttribute('type') + '</p>';
  38.     }
  39. }
  40.  
  41. // function to display xml created here
  42. function showMessage(xml)
  43. {
  44.     var list=xml.getElementsByTagName('tag');
  45.     document.getElementById('target').innerHTML = '';
  46.     for (var i=0;i<list.length;i++)
  47.     {
  48.         var node = list[i];
  49.         document.getElementById('target').innerHTML += '<p>' + node.firstChild.nodeValue + '</p>';
  50.     }
  51. }
  52.  
  53. // definition of the callback javascript class, used to handle async requests
  54. function callback() {}
  55. callback.prototype = {
  56.     createJunk: function(result) {
  57.         showItems(result);
  58.     },
  59.     writeDoc: function(result) {
  60.         dom = HTML_AJAX.grab('test.xml');
  61.         showMessage(dom);
  62.     }
  63. }
  64. // function used to clear out the target div
  65. function clearTarget() {
  66.     document.getElementById('target').innerHTML = 'clear';
  67. }
  68.  
  69. //create xml document to send back to server
  70. var xmlhello = '<' + '?xml version="1.0"?><root><tag>Hello</tag></root>';
  71. xmlhello = new DOMParser().parseFromString(xmlhello, 'text/xml');
  72.  
  73. var xmlgoodbye = '<' + '?xml version="1.0"?><root><tag>Goodbye</tag></root>';
  74. xmlgoodbye = new DOMParser().parseFromString(xmlgoodbye, 'text/xml');
  75.  
  76. </script>
  77. </head>
  78. <body>
  79. <script type="text/javascript">
  80. // create a proxy in sync mode
  81. var syncProxy = new TestXml();
  82. // create a proxy in async mode
  83. var asyncProxy = new TestXml(new callback());
  84.  
  85. // run a sync call and set its results to the target div
  86. function syncCall() {
  87.     dom = syncProxy.createHealthy();
  88.     showItems(dom);
  89. }
  90. function syncSend(xml) {
  91.     syncProxy.writeDoc(xml);
  92.     dom = HTML_AJAX.grab('test.xml');
  93.     showMessage(dom);
  94. }
  95.  
  96. // run a sync call, callback class will handle its results
  97. function asyncCall() {
  98.     asyncProxy.createJunk();
  99. }
  100. // run a sync call, callback class will handle its results
  101. function asyncSend(xml) {
  102.     asyncProxy.writeDoc(xml);
  103. }
  104. </script>
  105.  
  106. <p>HTML_AJAX XML functionality needs the Dom extensions in PHP5 or the DOMXML extension in PHP4.<br>
  107. It looks like you have:<br>
  108. <?php
  109. if (extension_loaded('Dom')) {
  110.     echo 'The Dom extension';
  111. }
  112. else if (extension_loaded('Domxml')) {
  113.     echo 'The Domxml extension';
  114. }
  115. else {
  116.     echo 'No XML DOM support, so you can expect these examples to fail';
  117. }
  118. ?>
  119. </p>
  120. <ul>
  121.     <li><a href="javascript:clearTarget()">Clear Target</a></li>
  122.     <li><a href="javascript:syncCall()">Retrieve XmlDom Sync</a></li>
  123.     <li><a href="javascript:asyncCall();">Retrieve XmlDom Async</a></li>
  124.     <li><a href="javascript:syncSend(xmlhello);">Send XmlDom Sync</a></li>
  125.     <li><a href="javascript:asyncSend(xmlgoodbye);">Send XmlDom Async</a></li>
  126. </ul>
  127.  
  128. <div style="white-space: pre; padding: 1em; margin: 1em; width: 600px; height: 300px; border: solid 2px black; overflow: auto;" id="target">Target</div>
  129. </div>
  130.  
  131. </body>
  132. </html>

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