Permalink
Browse files

Examples from local dev.

git-svn-id: http://svn.php.net/repository/pear/packages/HTML_AJAX/trunk@200178 c90b9560-bf6c-de11-be94-00142212c4b1
  • Loading branch information...
1 parent 19a1e19 commit a34ee17081baa614da387f09aa269b270e981518 David Coallier committed Nov 7, 2005
Showing with 197 additions and 0 deletions.
  1. +36 −0 examples/review/auto_server.php
  2. +136 −0 examples/review/index.php
  3. +25 −0 examples/review/review.class.php
@@ -0,0 +1,36 @@
+<?php
+/**
+ * Server that exposes a class for doing a fake review
+ *
+ * @category HTML
+ * @package AJAX
+ * @author Joshua Eichorn <josh@bluga.net>
+ * @copyright 2005 Joshua Eichorn
+ * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
+ * @version Release: @package_version@
+ * @link http://pear.php.net/package/HTML_AJAX
+ */
+
+ // include the server class
+include 'HTML/AJAX/Server.php';
+
+
+// extend HTML_AJAX_Server creating our own custom one with init{ClassName} methods for each class it supports calls on
+class ReviewServer extends HTML_AJAX_Server {
+ // this flag must be set to on init methods
+ var $initMethods = true;
+
+ // init method for the test class, includes needed files an registers it for ajax
+ function initReview() {
+ include 'review.class.php';
+ $this->registerClass(new review(),'Review',array('newReview','updateReview')); // specify methods so that we get case in php4
+ }
+}
+
+session_start();
+// create an instance of our test server
+$server = new ReviewServer();
+
+// handle requests as needed
+$server->handleRequest();
+?>
@@ -0,0 +1,136 @@
+<?php
+/**
+ * Almost real life example, show a list reviews, letting you add one, and then updating the list
+ *
+ * @category HTML
+ * @package AJAX
+ * @author Joshua Eichorn <josh@bluga.net>
+ * @copyright 2005 Joshua Eichorn
+ * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
+ * @version Release: @package_version@
+ * @link http://pear.php.net/package/HTML_AJAX
+ */
+
+// include the helper class
+require_once 'HTML/AJAX/Helper.php';
+
+// create an instance and set the server url
+session_start();
+$ajaxHelper = new HTML_AJAX_Helper();
+$ajaxHelper->serverUrl = 'auto_server.php';
+$ajaxHelper->jsLibraries[] = 'JSON'; // not included in the default list before 0.2.6
+
+?>
+<html>
+<head>
+
+<?php
+ // output a javascript neded to setup HTML_AJAX
+ // by default this is all the libraries shipped with HTML_AJAX, take a look at $ajaxHelper->jsLibraries to edit the list
+ echo $ajaxHelper->setupAJAX();
+
+ // ajax helper should do this for you but it doesn't yet
+?>
+<script type="text/javascript" src="auto_server.php?stub=review"></script>
+
+<script type="text/javascript">
+var reviewCallback = {
+ // after a review we get a chunk of html that we can update the reviewList with
+ newReview: function(result) {
+ document.getElementById('reviewList').innerHTML += result;
+ },
+ // after a review is updated we get a chunk of html, replace a node from our lookup list with that
+ updateReview: function(result) {
+ var newdiv = document.createElement('div');
+ newdiv.innerHTML = result[1];
+ document.getElementById('reviewList').replaceChild(newdiv.firstChild,reviewCallback.nodeList[result[0]]);
+ },
+ nodeList: []
+}
+function sendReview(form) {
+ var remoteReview = new Review(reviewCallback);
+ var payload = new Object();
+ for(var i = 0; i < form.elements.length; i++) {
+ if (form.elements[i].name) {
+ payload[form.elements[i].name] = form.elements[i].value;
+ }
+ }
+
+ // do any needed validation here
+
+ remoteReview.newReview(payload);
+}
+
+function updateReview(id,form) {
+ var remoteReview = new Review(reviewCallback);
+ var payload = new Object();
+ for(var i = 0; i < form.elements.length; i++) {
+ if (form.elements[i].name) {
+ payload[form.elements[i].name] = form.elements[i].value;
+ }
+ }
+
+ // do any needed validation here
+
+ remoteReview.updateReview(id,payload);
+}
+
+
+function editReview(id,node) {
+ var form = document.getElementById('reviewForm').cloneNode(true);
+ form.onsubmit = function() { updateReview(id,this); return false; }
+
+ var data = new Object();
+ var divs = node.getElementsByTagName('div');
+ for(var i = 0; i < divs.length; i++) {
+ if (divs[i].className) {
+ data[divs[i].className] = divs[i].innerHTML;
+ }
+ }
+
+ for(var i = 0; i < form.elements.length; i++) {
+ if (form.elements[i].name) {
+ form.elements[i].value = data[form.elements[i].name];
+ }
+ }
+
+ document.getElementById('reviewList').replaceChild(form,node);
+ reviewCallback.nodeList[id] = form;
+}
+</script>
+
+<style type="text/css">
+.name {
+ margin-top: 5px;
+ background-color: #ddd;
+}
+#reviewList {
+ width: 300px;
+}
+</style>
+
+</head>
+<body>
+
+<h2>Add a new Review</h2>
+<form id="reviewForm" onsubmit="sendReview(this); return false;" style="border: dotted 1px black">
+
+Your Name: <input name="name"><br>
+Your Review: <textarea name="review"></textarea><br>
+<input type="submit">
+</form>
+
+<div id="reviewList">
+<?php
+ if (isset($_SESSION['reviews'])) {
+ foreach($_SESSION['reviews'] as $key => $review) {
+ echo "<div onclick='editReview($key,this)'><div class='name'>$review->name</div><div class='review'>$review->review</div></div>\n";
+ }
+ }
+?>
+</div>
+</body>
+</html>
+<?php
+/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
+?>
@@ -0,0 +1,25 @@
+<?php
+class Review {
+
+ // constructor won't be exported
+ function Review() {
+ if (!isset($_SESSION['reviews'])) {
+ $_SESSION['reviews'] = array();
+ }
+ }
+
+ // data is an array of objects
+ function newReview($data) {
+ $_SESSION['reviews'][] = $data;
+
+ $key = count($_SESSION['reviews'])-1;
+
+ return "<div onclick='editReview($key,this)'><div class='name'>$data->name</div><div class='review'>$data->review</div></div>";
+ }
+
+ function updateReview($key,$data) {
+ $_SESSION['reviews'][$key] = $data;
+ return array($key,"<div onclick='editReview($key,this)'><div class='name'>$data->name</div><div class='review'>$data->review</div></div>");
+ }
+}
+?>

0 comments on commit a34ee17

Please sign in to comment.