[ Index ]

PHP Cross Reference of JPSpan 0.4 (beta)

title

Body

[close]

/examples/ -> roundtrip.php (source)

   1  <?php
   2  header ('Content-Type: text/html; charset=UTF-8');
   3  
   4  // $Id: roundtrip.php,v 1.4 2004/11/16 21:03:50 harryf Exp $
   5  require_once  '../JPSpan.php';
   6  
   7  // Compress the Javascript
   8  define('JPSPAN_INCLUDE_COMPRESS',TRUE);
   9  require_once JPSPAN . 'Include.php';
  10  JPSpan_Include_Register('util/data.js');
  11  JPSpan_Include_Register('encode/php.js');
  12  JPSpan_Include_Register('encode/xml.js');
  13  
  14  function path() {
  15      $basePath = explode('/',$_SERVER['SCRIPT_NAME']);
  16      $script = array_pop($basePath);
  17      $basePath = implode('/',$basePath);
  18      if ( isset($_SERVER['HTTPS']) ) {
  19          $scheme = 'https';
  20      } else {
  21          $scheme = 'http';
  22      }
  23      echo $scheme.'://'.$_SERVER['SERVER_NAME'].$basePath;
  24  }
  25  
  26  ?>
  27  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  28  <html>
  29  <head>
  30  <title> Round Trips </title>
  31  <script type="text/javascript">
  32  <!--
  33  <?php JPSpan_Includes_Display(); ?>
  34  
  35  var printrURL = "<?php path();?>/printrresponse.php";
  36  var serializedURL = "<?php path();?>/serializedresponse.php";
  37  
  38  //------------------------------------------------------------
  39  function MyObject() {}
  40  
  41  //------------------------------------------------------------
  42  function var_dump(data) {
  43      var Data = new JPSpan_Util_Data();
  44      return Data.dump(data);
  45  }
  46  
  47  //------------------------------------------------------------
  48  function encode(data) {
  49      if ( document.getElementById("rencoding").value == 'php' ) {
  50          var encoder = new JPSpan_Encode_PHP();
  51      } else {
  52          var encoder = new JPSpan_Encode_Xml();
  53      }
  54      return encoder.encode(data);
  55  }
  56  
  57  //------------------------------------------------------------
  58  function send(targetURL,data) {
  59      var httpRequest = false;
  60      try {
  61          httpRequest=new ActiveXObject("Msxml2.XMLHTTP")
  62      } catch (e) {
  63          try {
  64              httpRequest=new ActiveXObject("Microsoft.XMLHTTP")
  65          } catch (e) {
  66              try {
  67                  httpRequest = new XMLHttpRequest();
  68              } catch (e) {
  69                  alert ('Your browser does not support XMLHttpRequest');
  70                  return null;
  71              }
  72          }
  73      }
  74      httpRequest.open("POST", targetURL, false, null, null);
  75      httpRequest.setRequestHeader("Content-Length", data.length);
  76      httpRequest.send(data);
  77      if (httpRequest.status!=200) {
  78          alert("Url: "+targetURL+" not found");
  79          return null;
  80      }    
  81      return httpRequest.responseText;   
  82  }
  83  
  84  //------------------------------------------------------------
  85  function clear () {
  86      document.getElementById('results').innerHTML='';
  87  }
  88  
  89  //------------------------------------------------------------
  90  function echo(msg, out) {
  91      document.getElementById("results").innerHTML += "<b>"+msg+"</b>";
  92      if ( document.getElementById("var_dump").checked ) {
  93          out = var_dump(out);
  94      }
  95      if ( document.getElementById("rencoding").value == 'xml' ) {
  96          out = out.replace(/&/g, '&amp;').replace(/</g, '&lt;');
  97      }
  98      document.getElementById('results').innerHTML += '<pre>'+out+'</pre>';
  99  }
 100  
 101  //------------------------------------------------------------
 102  function printR() {
 103      var value = document.getElementById('name').value;
 104      value = encode(value);
 105      echo("Request:",value);
 106      var serverurl = printrURL+'?rencoding='+document.getElementById("rencoding").value;
 107      var response = send(serverurl,value);
 108      echo("Response:",response);
 109  }
 110  
 111  //------------------------------------------------------------
 112  function arrayR() {
 113      var a = new Array();
 114      a[0] = 'x';
 115      a[1] = 'y';
 116      a.foo = 'bar';
 117      var value = encode(a);
 118      echo("Request:",value);
 119      var serverurl = printrURL+'?rencoding='+document.getElementById("rencoding").value;
 120      var response = send(serverurl,value);
 121      echo("Response:",response);
 122  }
 123  
 124  //------------------------------------------------------------
 125  function serialized() {
 126      var value = document.getElementById('name').value;
 127      value = encode(value);
 128      echo("Request:",value);
 129      var serverurl = serializedURL+'?rencoding='+document.getElementById("rencoding").value;
 130      var response = send(serverurl,value);
 131      var datafunc = eval(response);
 132      var data = datafunc();
 133      echo("Response:",data);
 134  }
 135  
 136  //------------------------------------------------------------
 137  function sendObject() {
 138      obj = new Object();
 139      obj.x = 'Foo';
 140      obj.y = 'Bar';
 141      obj.z = ['a','b','c'];
 142      var value = encode(obj);
 143      echo("Request:",value);
 144      var serverurl = serializedURL+'?rencoding='+document.getElementById("rencoding").value;
 145      var response = send(serverurl,value);
 146      var datafunc = eval(response);
 147      var data = datafunc();
 148      echo("Response",data);
 149  }
 150  
 151  //------------------------------------------------------------
 152  function getError() {
 153      var obj = new Object();
 154      obj['geterror'] = 'standarderror';
 155      var value = encode(obj);
 156      echo("Request:",value);
 157      var serverurl = serializedURL+'?rencoding='+document.getElementById("rencoding").value;
 158      var response = send(serverurl,value);
 159      var datafunc = eval(response);
 160      try {
 161          var data = datafunc();
 162          echo("Response:",data);
 163      } catch (e) {
 164          alert (e.name+": "+e.message);
 165          echo("Error (expected):",e);
 166      }
 167  }
 168  
 169  //------------------------------------------------------------
 170  function getMyError() {
 171      var obj = new Object();
 172      obj['geterror'] = 'customerror';
 173      var value = encode(obj);
 174      echo("Request:",value);
 175      var value = encode(obj);
 176      echo("Request:",value);
 177      var serverurl = serializedURL+'?rencoding='+document.getElementById("rencoding").value;
 178      var response = send(serverurl,value);
 179      var datafunc = eval(response);
 180      try {
 181          var data = datafunc();
 182          echo("Response",data);
 183      } catch (e) {
 184          alert (e.name+": "+e.message);
 185          echo("Error (expected):",e);
 186      }
 187  }
 188  -->
 189  </script>
 190  </head>
 191  <body>
 192  <h1>Round Trips</h1>
 193  <p>Demonstrates making a request to a remote PHP script using XMLHttpRequest. Be warned requests are synchronous
 194  - on Sourceforge with network latency / server issues this may hang your browser (esp. IE).
 195  Side note: excuse the abuse of the acronym tag - laziness.</p>
 196  <form>
 197  Enter some text: <input id="name" type="text" value="Joe Bloggs (Iñtërnâtiônàlizætiøn)" size="35">
 198  <acronym title="Text in this field will be used in some of the examples below">?</acronym>
 199  <br>
 200  var_dump: <input id="var_dump" type="checkbox" checked>
 201  <acronym title="Request and response will be shown with data typing information">?</acronym>
 202  <br>
 203  request serialization: <select id="rencoding"><option>php</option><option selected>xml</option></select>
 204  <acronym title="Notice the effect on the word Iñtërnâtiônàlizætiøn when using the PHP serializatation">?</acronym>
 205  <br>
 206  </form>
 207  <a href="javascript:clear();">Clear</a> 
 208  <acronym title="Clear the results">?</acronym> |
 209  <a href="javascript:printR()">Get back what you sent</a> 
 210  <acronym title="Server will unserialize the request then send back the result passed through PHPs print_r() function">?</acronym> |
 211  <a href="javascript:arrayR()">Associative Array</a> 
 212  <acronym title="Server will unserialize the request then send back the result passed through PHPs print_r() function">?</acronym> |
 213  <a href="javascript:serialized()">Serialized Response</a> 
 214  <acronym title="Server will return an encoded response">?</acronym> |
 215  <a href="javascript:sendObject()">Send an Object</a>
 216  <acronym title="Server will return an encoded response">?</acronym> |
 217  <a href="javascript:getError()">Get Error</a> 
 218  <acronym title="Server will return an encoded Javascript exception">?</acronym> |
 219  <a href="javascript:getMyError()">Get Custom Error</a> 
 220  <acronym title="Server will return an encoded Javascript exception (extending the error)">?</acronym> |
 221  <h2>Results</h2>
 222  <div id="results">
 223  </div>
 224  </body>
 225  </html>


Generated: Fri Nov 26 11:42:46 2004 Cross-referenced by PHPXref 0.6