Commit 5023cf988e28a901dc1b9bf545ad9807f7a736a6

Authored by Georg Hopp
1 parent 5e96db2b

ajax tests hinzugefuegt

No preview for this file type
  1 +<?php
  2 +
  3 + $english = mysql_escape_string($_REQUEST['translate']);
  4 +// Der Service ist zur Zeit leider deaktiviert....
  5 +// $trans = new SoapClient(
  6 +// "http://www.xmethods.net/sd/2001/BabelFishService.wsdl");
  7 +
  8 +/*
  9 + try
  10 + {
  11 + $german = $trans->BabelFish("en_de",$english);
  12 + $french = $trans->BabelFish("en_fr",$english);
  13 + }
  14 + catch(SoapFault $e)
  15 + {
  16 + $english = "not found";
  17 + $german = "not found";
  18 + $french = "not found";
  19 + }
  20 +*/
  21 + $encoding = FALSE;
  22 + if (isset ($_SERVER['HTTP_ACCEPT_ENCODING']) &&
  23 + strpos ($_SERVER['HTTP_ACCEPT_ENCODING'], 'x-gzip') !== FALSE)
  24 + $encoding = "x-gzip";
  25 + if (isset ($_SERVER['HTTP_ACCEPT_ENCODING']) &&
  26 + strpos ($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE)
  27 + $encoding = "gzip";
  28 +
  29 + header ('Content-type: text/plain');
  30 +
  31 + $result = FALSE;
  32 + if ($encoding !== FALSE)
  33 + $result = gzcompress (json_encode (array(
  34 + "english" => 'doing gzip',
  35 + "german" => 'gezipedte Daten',
  36 + "french" => 'la zippo')));
  37 +
  38 + if ($result !== FALSE)
  39 + {
  40 + header ('Content-Encoding: ' . $encoding);
  41 +
  42 + print ("\x1f\x8b\x08\x00\x00\x00\x00\x00");
  43 + print ($result);
  44 + }
  45 + else
  46 + {
  47 + $result = json_encode (array(
  48 + "english" => 'doing no gzip',
  49 + "german" => 'nicht gezipedte Daten',
  50 + "french" => 'no la zippo'));
  51 +
  52 + print ($result);
  53 + }
  54 +
  55 +?>
  1 +<html>
  2 + <head>
  3 + <title>Meine ersten Ajax Erfahrungen</title>
  4 + <meta http-equiv="content-type" content="text/html;charset=utf-8" />
  5 +
  6 + <script type="text/javascript">
  7 + <!--
  8 + var req = null;
  9 +
  10 + function handleTranslation ()
  11 + {
  12 + switch (req.readyState)
  13 + {
  14 + case 4:
  15 + if (req.status != 200)
  16 + alert("Fehler:"+req.status);
  17 + else
  18 + {
  19 + // felder des formulars
  20 + german_field = document.getElementById("german");
  21 + french_field = document.getElementById("french");
  22 +
  23 + // antwort des servers
  24 + var translation = eval('(' + req.responseText + ')');
  25 +
  26 + // schreiben des ergebnisses
  27 + german_field.value = translation.german;
  28 + french_field.value = translation.french;
  29 + }
  30 + break;
  31 +
  32 + default:
  33 + return false;
  34 + break;
  35 + }
  36 + }
  37 +
  38 + function translate ()
  39 + {
  40 + // erstellen des requests
  41 +
  42 + try
  43 + {
  44 + req = new XMLHttpRequest();
  45 + }
  46 + catch (e)
  47 + {
  48 + try
  49 + {
  50 + req = new ActiveXObject("Msxml2.XMLHTTP");
  51 + }
  52 + catch (e)
  53 + {
  54 + try
  55 + {
  56 + req = new ActiveXObject("Microsoft.XMLHTTP");
  57 + }
  58 + catch (failed)
  59 + {
  60 + req = null;
  61 + }
  62 + }
  63 + }
  64 +
  65 + if (req == null)
  66 + alert("Error creating request object!");
  67 +
  68 + // anfrage erstellen (GET, url ist localhost,
  69 + // request ist asynchron
  70 + var url = 'http://muedv031/~georg/bilder/ajax+json/ajax.php' +
  71 + '?translate=' + document.getElementById ('myword').value;
  72 +
  73 + req.open("GET", url, true);
  74 +
  75 + // Beim abschliessen des request wird diese Funktion ausgeführt
  76 + req.onreadystatechange = handleTranslation;
  77 +
  78 + req.setRequestHeader("Content-Type",
  79 + "application/x-www-form-urlencoded");
  80 +
  81 + req.send(null);
  82 + }
  83 +
  84 + //-->
  85 + </script>
  86 + </head>
  87 +
  88 + <body>
  89 + <div id="eins" style="width: 80%; height: 80%; border: dashed 1px;">
  90 + englisch: <input type="text" id="myword" onchange="translate();" />
  91 + <br />
  92 + deutsch: <input type="text" id="german" /><br />
  93 + französisch: <input type="text" id="french" /><br />
  94 + </div>
  95 + </body>
  96 +</html>
Please register or login to post a comment