AJAX
[ class tree: AJAX ] [ index: AJAX ] [ all elements ]

Source for file index.php

Documentation is available at index.php

  1. <?php
  2. /**
  3.  * A simple guestbook with the goal of not a line of javascript :)
  4.  *
  5.  * @category   HTML
  6.  * @package    AJAX
  7.  * @author     Elizabeth Smith <auroraeosrose@gmail.com>
  8.  * @copyright  2005 Elizabeth Smith
  9.  * @license    http://www.opensource.org/licenses/lgpl-license.php  LGPL
  10.  * @version    Release: 0.5.4
  11.  * @link       http://pear.php.net/package/HTML_AJAX
  12.  */
  13.  
  14. //require the helper class - it will take care of everything else
  15. require_once 'HTML/AJAX/Helper.php';
  16.  
  17. //since we're not REALLY using a backend like a database, use sessions to store data
  18. //set up HTML_AJAX_Helper
  19. $ajaxHelper = new HTML_AJAX_Helper();
  20. //tell it what url to use for the server
  21. $ajaxHelper->serverUrl = 'auto_server.php';
  22. //add haserializer to set
  23. $ajaxHelper->jsLibraries['haserializer';
  24. // Open tags problem... I know ugly.
  25. $ajaxHelper->stubs['guestbook';
  26.  
  27. print '<?xml version="1.0" encoding="utf-8"?>';
  28. ?>
  29.  
  30. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN
  31.    "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
  32. <html>
  33. <head>
  34. <title>My Guestbook</title>
  35. <?php
  36.     // output a javascript neded to setup HTML_AJAX
  37.     echo $ajaxHelper->setupAJAX();
  38. ?>
  39. <script type="text/javascript">
  40. HTML_AJAX.onError = function(e) { alert(HTML_AJAX_Util.quickPrint(e)); }
  41. </script>
  42. <style type="text/css">
  43. body {
  44. color: #24006B;
  45. }
  46. h2 {
  47.   text-align: center;
  48.   color: #330099;
  49.   }
  50. #guestbookForm, #guestbookList {
  51.   width: 65%;
  52.   margin-right: auto;
  53.   margin-left: auto;
  54.   padding: 1.5em;
  55.   margin-top: 10px;
  56.   background-color: #D5BFFF;
  57.   border: 4px double #FFCC00;
  58. }
  59. fieldset, div.entry {
  60. background-color: #FFF2BF;
  61. border: 4px double #330099;
  62. padding: 0.5em;
  63. }
  64. legend {
  65. color: #330099;
  66. font-size: 0.8em;
  67. font-style: italic;
  68. }
  69. label {
  70. clear: both;
  71. display: block;
  72. float: left;
  73. width: 20%;
  74. text-align: right;
  75. font-weight: bold;
  76. }
  77. input {
  78. display: block;
  79. float: left;
  80. width: 40%;
  81. margin: 0 0.5em 0.5em 0.5em;
  82. background-color: #B38F00;
  83. border: 1px solid #AA80FF;
  84. color: #330099;
  85. }
  86. input:focus, textarea:focus {
  87. background-color: #D5BFFF;
  88. border: 1px solid #B38F00;
  89. }
  90. textarea {
  91. display: block;
  92. float: left;
  93. width: 40%;
  94. height: 10em;
  95. margin: 0 0.5em 0.5em 0.5em;
  96. background-color: #B38F00;
  97. border: 1px solid #AA80FF;
  98. color: #330099;
  99. }
  100. input[type="submit"] {
  101. display: block;
  102. width: auto;
  103. float: none;
  104. margin-right: auto;
  105. margin-left: auto;
  106. font-size: 1.5em;
  107. font-weight: bold;
  108. background-color: #330099;
  109. color: #FFCC00;
  110. border: 3px double #FFE680;
  111. }
  112. .error {
  113. color: #CC0000;
  114. font-weight: bold;
  115. float: left;
  116. }
  117. .small {
  118. font-size: 0.8em
  119. }
  120. </style>
  121.  
  122. </head>
  123. <body>
  124. <?php //eventually ajax_helper should set this up for you, it's not done yet?>
  125. <script type="text/javascript">
  126. function sendguestbook(form) {
  127.     var remoteguestbook = new guestbook();
  128.     var payload = new Object();
  129.     for(var i = 0; i < form.elements.length; i++) {
  130.         if (form.elements[i].name) {
  131.  
  132.             payload[form.elements[i].name] = form.elements[i].value;
  133.         }
  134.     }
  135.     remoteguestbook.newEntry(payload);
  136.     return false;
  137. }
  138. function clearguestbook() {
  139.     var remoteguestbook = new guestbook();
  140.     remoteguestbook.clearGuestbook();
  141. }
  142. function deleteentry(id) {
  143.     var remoteguestbook = new guestbook();
  144.     remoteguestbook.deleteEntry(id);
  145. }
  146. function editentry(id) {
  147.     var remoteguestbook = new guestbook();
  148.     remoteguestbook.editEntry(id);
  149. }
  150. function updateselect(id) {
  151.     var remoteguestbook = new guestbook();
  152.     remoteguestbook.updateSelect(id);
  153. }
  154. </script>
  155. <h2>Welcome to the Guestbook</h2>
  156. <div id="guestbookList">
  157. <?php
  158.  
  159.     if (isset($_SESSION['entries'])) {
  160.         foreach($_SESSION['entries'as $key => $data{
  161.            echo '<div class="entry" id="entry'.$key.'">'
  162.             .'<h3><a href="mailto:'.$data->email.'">'.$data->name.'</a></h3>';
  163.             if(!empty($data->website))
  164.             {
  165.                 echo '<a href="http://'.$data->website.'">'.$data->website.'</a><br />';
  166.             }
  167.             echo '<p>'.$data->comments.'</p>'
  168.             .'<div class="small">Posted: '.$data->date.' | '
  169.             .'<a href="#" onclick="editentry('.$key.');">Edit</a> | '
  170.             .'<a href="#" onclick="deleteentry('.$key.');">Delete</a></div>'
  171.             .'</div>';
  172.         }
  173.     }
  174. ?>
  175. </div>
  176.  
  177. <div><a href="#" onclick="clearguestbook(this);">Clear Guestbook</a></div>
  178.  <form id="guestbookForm" action="index.php" method="post" onsubmit="sendguestbook(this); return false;">
  179.   <fieldset>
  180.    <legend>Leave Your Comments</legend>
  181.     <label for="name">Name: </label><input name="name" id="name" />
  182.     <label for="email">Email: </label><input name="email" id="email" />
  183.     <label for="website">Website: </label><input name="website" id="website" />
  184.     <label for="comments">Comments: </label><textarea name="comments" id="comments"></textarea>
  185.     <br style="clear: both" />
  186.     <input type="submit" id="submit" name="submit" value="Add Comments" />
  187.   </fieldset>
  188.  </form>
  189.  
  190. <p>Fill a select item with a list of options - tests the HTML_AJAX_Action::replaceNode and HTML_AJAX_Action::createNode methods</p>
  191.  <form id="testing" action="index.php" method="post" onsubmit="return false;">
  192.   <div>
  193.     <a href="#" onclick="updateselect('replaceme');">Gimme some options</a>
  194.     <select id="replaceme">
  195.         <option name="dog" id="dog">Dog</option>
  196.     </select>
  197.   </div>
  198.  </form>
  199.  
  200. </body>
  201. </html>

Documentation generated on Fri, 04 Apr 2008 18:30:17 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.