Add AJAX example from old code/ pages.
authorW. Trevor King <wking@drexel.edu>
Wed, 13 Oct 2010 21:26:04 +0000 (17:26 -0400)
committerW. Trevor King <wking@drexel.edu>
Wed, 13 Oct 2010 21:26:04 +0000 (17:26 -0400)
ikiwiki.setup
posts/AJAX.mdwn [new file with mode: 0644]
posts/AJAX/data.xml [new file with mode: 0644]
posts/AJAX/example.html [new file with mode: 0644]
posts/AJAX/lookup.php [new file with mode: 0644]

index 9982a47b9844874f4381045152f98c35081fdfec..8f9dcb3921ae2be603301d160951320e6b19de1a 100644 (file)
@@ -29,7 +29,7 @@ use IkiWiki::Setup::Standard {
        # rcs backend to use
        rcs => 'git',
        # plugins to add to the default configuration
-       add_plugins => [qw{goodstuff linktoimgonly lockedit mdwn_itex org sidebar}],
+       add_plugins => [qw{goodstuff linktoimgonly lockedit mdwn_itex org sidebar rawhtml}],
        # plugins to disable
        disable_plugins => [qw{editpage passwordauth smiley}],
        # location of template files
diff --git a/posts/AJAX.mdwn b/posts/AJAX.mdwn
new file mode 100644 (file)
index 0000000..301a262
--- /dev/null
@@ -0,0 +1,21 @@
+[[!meta  title="AJAX"]]
+[[!meta  date="2009-09-19 16:27:00"]]
+
+[AJAX][] is a web-buzzword that I had always thought must be more
+complicated than it really is.  I finally got a chance to look into it
+and write up a simple [[example]].  The example
+
+[[!inline pagenames="example" template="raw" feeds="no"]]
+
+accesses the server-side
+
+[[!inline pagenames="data.xml" template="raw" feeds="no"]]
+
+via
+
+[[!inline pagenames="lookup.php" template="raw" feeds="no"]]
+
+[AJAX]: http://en.wikipedia.org/wiki/Ajax_(programming)
+
+[[!tag tags/programming]]
+[[!tag tags/web]]
diff --git a/posts/AJAX/data.xml b/posts/AJAX/data.xml
new file mode 100644 (file)
index 0000000..19069db
--- /dev/null
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8" ?> 
+<dictionary>
+  <entry>
+    <word>ping</word>
+    <definition>A common dummy word for testing functionality.</definition>
+  </entry>
+  <entry>
+    <word>dummy</word>
+    <definition>One word seemed too silly...</definition>
+  </entry>
+</dictionary>
diff --git a/posts/AJAX/example.html b/posts/AJAX/example.html
new file mode 100644 (file)
index 0000000..d69980b
--- /dev/null
@@ -0,0 +1,49 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+<head>
+<script language="javascript" type="text/javascript">
+function getWord() {
+  return document.getElementById('word').value;
+}
+function setDef(text) {
+  document.getElementById('def').innerHTML = text;
+}
+function getHTTPObject() {
+  if (window.ActiveXObject)
+    return new ActiveXObject("Microsoft.XMLHTTP");
+  else if (window.XMLHttpRequest)
+    return new XMLHttpRequest();
+  else {
+    setDef("Your browser does not support AJAX.");
+    return NULL;
+  }
+}
+function doAJAX() {
+  httpObject = getHTTPObject();
+  if (httpObject != null) {
+    httpObject.open("GET", "lookup.php?word=" + getWord(), true);
+    httpObject.send(null); // request is on it's way...
+    setDef("Sent request for" + getWord());
+    // here's the asynchronous read:
+    httpObject.onreadystatechange = function() { respondAJAX(); }
+  }
+}
+function respondAJAX(){
+  if(httpObject.readyState == 4) {
+    setDef(httpObject.responseText);
+  }
+}
+</script>
+</head>
+<body>
+  <form action="never_call_this_invalid_page.html" method="get">
+    <p>
+      Word: <input type="text" name="word" id="word" onBlur="doAJAX();"/><br />
+      <input type="submit" value="Submit" />
+    </p>
+    <div name="def" id="def"></div>
+  </form>
+</body>
+</html>
diff --git a/posts/AJAX/lookup.php b/posts/AJAX/lookup.php
new file mode 100644 (file)
index 0000000..e8c0472
--- /dev/null
@@ -0,0 +1,23 @@
+<?php
+// generate an xml snippet with the requested information.
+// stays server-side.
+
+$word = $_GET['word'];
+
+$ret = '';
+
+$f = simplexml_load_file('data.xml');
+foreach ($f as $entry) {
+  if ($entry->word == $word) {
+    $ret .= '<result>'.$entry->definition.'</result>';
+    // this doesn't even have to be XML, but for official "AJAX"
+    break;
+  }
+}
+if (strlen($ret) == 0) {
+  $ret .= '<result>'.$word.' not found</result>';
+}
+echo '<?xml version="1.0" encoding="UTF-8" ?>';
+echo $ret;
+
+?>