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

Source for file index.php

Documentation is available at index.php

  1. <?php
  2.     require_once 'HTML/AJAX/Helper.php';
  3.     $objAjaxHelper = new HTML_AJAX_Helper();
  4.     $objAjaxHelper->serverUrl = './php/auto_server.php';
  5.     $objAjaxHelper->jsLibraries['haserializer';
  6.     $objAjaxHelper->stubs['login';
  7.     $strAjaxScript $objAjaxHelper->setupAJAX();
  8. ?>
  9. <html>
  10. <head>
  11.     <title>Form validation (v2) with HTML_AJAX</title>
  12.     <!-- HTML_AJAX -->
  13.     <?php echo $strAjaxScript ?>
  14.     <script type="text/javascript">
  15.         /**
  16.          * Basic page initialization
  17.          */
  18.         function initPage() {
  19.             // Set up the labels so they know the associated input elements
  20.             var arrLabels = document.getElementsByTagName("label");
  21.             for (var i=0; i < arrLabels.length; i++) {
  22.                 var objTemp = arrLabels[i];
  23.                 var strFor = objTemp.getAttribute('for');
  24.  
  25.                 // Fix the attributes
  26.                 if (strFor != '') {
  27.                     // Set the ID of the label
  28.                     objTemp.setAttribute('id', 'l' + strFor);
  29.  
  30.                     // Save the original class of the label (if any)
  31.                     objTemp.setAttribute('classOrig', objTemp.getAttribute('class'));
  32.                 }
  33.             }
  34.  
  35.             // Set the focus on the first element
  36.             document.getElementById('username').focus();
  37.         }
  38.  
  39.         /**
  40.          * Sets the class of an element (build for this example)
  41.          */
  42.         function setElement(strElement, blnValidated) {
  43.             // Update the label
  44.             var objElem = document.getElementById('l' + strElement);
  45.             if (objElem) {
  46.                 if (blnValidated == 1) {
  47.                     strClass = objElem.getAttribute('classOrig');
  48.                 } else {
  49.                     strClass = 'error';
  50.                 }
  51.                 objElem.setAttribute('class', '' + strClass);
  52.             }
  53.  
  54.             return false;
  55.         }
  56.  
  57.         /**
  58.          * Shows or hides an element
  59.          */
  60.         function toggleElement(strElement, blnVisible) {
  61.             var objStyle = document.getElementById(strElement).style;
  62.             if (objStyle) {
  63.                 objStyle.display = (blnVisible == 1) ? 'block' : 'none';
  64.             }
  65.         }
  66.  
  67.         /**
  68.          * Login function
  69.          */
  70.         function doLogin() {
  71.             // Create object with values of the form
  72.             var objTemp = new Object();
  73.             objTemp['username'] = document.getElementById('username').value;
  74.             objTemp['password'] = document.getElementById('password').value;
  75.             objTemp['email'] = document.getElementById('email').value;
  76.  
  77.             // Create a dummy callback so the loading box will appear...
  78.             var objCallback = { validate: function() {} };
  79.  
  80.             // Send the object to the remote class
  81.             var objLogin = new login(objCallback);
  82.             objLogin.validate(objTemp);
  83.         }
  84.     </script>
  85.  
  86.     <!-- STYLESHEET  -->
  87.     <link rel="stylesheet" type="text/css" href="login.css" />
  88. </head>
  89. <body>
  90.     <!-- THE ERROR MESSAGES -->
  91.     <div id="messages" class="errorbox"></div>
  92.     <br />
  93.  
  94.     <!-- THE FORM -->
  95.     <form action="" method="post" onSubmit="doLogin(); return false;">
  96.         <fieldset style="width: 500px;">
  97.             <legend>Enter your login details</legend>
  98.  
  99.             <table width="400" border="0" cellspacing="0" cellpadding="2">
  100.                 <tr>
  101.                     <td><label for="username">Username:</label></td>
  102.                     <td><input type="text" name="username" id="username" size="40" tabindex="1"></td>
  103.                 </tr>
  104.                 <tr>
  105.                     <td><label for="password">Password:</label></td>
  106.                     <td><input type="text" name="password" id="password" size="40" tabindex="2"></td>
  107.                 </tr>
  108.                 <tr>
  109.                     <td><label for="email">E-mail:</label></td>
  110.                     <td><input type="text" name="email" id="email" size="40" tabindex="3"></td>
  111.                 </tr>
  112.                 <tr>
  113.                     <td>&nbsp;</td>
  114.                     <td><input type="submit" value="  login  ">&nbsp;<input type="reset" value="  reset  "></td>
  115.                 </tr>
  116.             </table>
  117.         </fieldset>
  118.     </form>
  119.     <div id="HTML_AJAX_LOADING" class="AjaxLoading">Please wait while loading</div>
  120.  
  121.     <p>
  122.         <strong>This sample is an updated version of the original login sample</strong>:<br />
  123.         &#0187; It now uses the new HTML_AJAX_Action class.<br>
  124.         &#0187; The initialization of the labels and their input elements is done by Javascript to clean up the page (function: initPage()).<br>
  125.         <br>
  126.  
  127.         <strong>Design notes</strong>:<br>
  128.         &#0187; The attribute &quot;style.display&quot; cannot be set from php code. Don't know why though. I created a wrapper function for it called &quot;toggleElement&quot; which does the trick. Funny enough when i execute thesame lines of script using -&gt;insertScript nothing happens.<br>
  129.         &#0187; You have to add a dummy callback in order to show the loading progress bar.<br>
  130.         &#0187; Enter username &quot;peter&quot;, password &quot;gabriel&quot; and any valid e-mail adress to see a messagebox.<br>
  131.  
  132.         <br>
  133.         &copy; under LGPL @ 21 nov 2005 by www.webunity.nl<br>
  134.     </p>
  135.  
  136.     <script type="text/javascript">
  137.         if (document.getElementsByTagName) {
  138.             initPage();
  139.         } else {
  140.             alert('This sample requires the DOM2 "getElementsByTagName" function.');
  141.         }
  142.     </script>
  143. </body>
  144. </html>
  145. <?php
  146. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  147. ?>

Documentation generated on Mon, 11 Mar 2019 15:59:25 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.