Showing
2 changed files
with
163 additions
and
0 deletions
ajax+json@home/ajax.php
0 → 100644
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 | + class dummy | ||
22 | + { | ||
23 | + // nur public member werden via json verschickt. | ||
24 | + public $german; | ||
25 | + public $english; | ||
26 | + public $french; | ||
27 | + | ||
28 | + function __construct ($english, $german, $french) | ||
29 | + { | ||
30 | + $this->german = $german; | ||
31 | + $this->english = $english; | ||
32 | + $this->french["val1"] = $french; | ||
33 | + $this->french["val2"] = "jokus"; | ||
34 | + } | ||
35 | + } | ||
36 | + | ||
37 | + $encoding = FALSE; | ||
38 | + if (isset ($_SERVER['HTTP_ACCEPT_ENCODING']) && | ||
39 | + strpos ($_SERVER['HTTP_ACCEPT_ENCODING'], 'x-gzip') !== FALSE) | ||
40 | + $encoding = "x-gzip"; | ||
41 | + if (isset ($_SERVER['HTTP_ACCEPT_ENCODING']) && | ||
42 | + strpos ($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE) | ||
43 | + $encoding = "gzip"; | ||
44 | + | ||
45 | + header ('Content-type: text/plain'); | ||
46 | + | ||
47 | + $result = FALSE; | ||
48 | + if ($encoding !== FALSE) | ||
49 | + $result = gzcompress (json_encode (new dummy ( | ||
50 | + 'doing gzip', 'gezipedte Daten', 'la zippo'))); | ||
51 | + | ||
52 | + if ($result !== FALSE) | ||
53 | + { | ||
54 | + header ('Content-Encoding: ' . $encoding); | ||
55 | + | ||
56 | + print ("\x1f\x8b\x08\x00\x00\x00\x00\x00"); | ||
57 | + print ($result); | ||
58 | + } | ||
59 | + else | ||
60 | + { | ||
61 | + $result = json_encode (new dummy ( | ||
62 | + 'doing no gzip', 'nicht gezipedte Daten', 'no la zippo')); | ||
63 | + | ||
64 | + print ($result); | ||
65 | + } | ||
66 | + | ||
67 | +?> |
ajax+json@home/test1.html
0 → 100644
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.val1; | ||
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