r7093: - added a new Form() ejs object for producing simple forms.
authorAndrew Tridgell <tridge@samba.org>
Mon, 30 May 2005 08:13:34 +0000 (08:13 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 18:17:15 +0000 (13:17 -0500)
- tidied up the html generation a bit

swat/esptest/exception.esp
swat/esptest/formtest.esp
swat/esptest/session.esp
swat/esptest/showvars.esp
swat/login.esp
swat/scripting/common.js

index abea890d987122faa53e90400b4008d9b3bec889..cfd0f312fab86ab928376ca99b9534baebd1ba0b 100644 (file)
@@ -5,16 +5,10 @@
 <form name="ExceptionTest" method="POST" action="@@request['SCRIPT_NAME']"> 
     <input name="submit" type="submit" value="Generate Exception">
     <input name="submit" type="submit" value="No Exception">
 <form name="ExceptionTest" method="POST" action="@@request['SCRIPT_NAME']"> 
     <input name="submit" type="submit" value="Generate Exception">
     <input name="submit" type="submit" value="No Exception">
-    <input name="submit" type="submit" value="Cancel"><br>
 </form>
 
 <%
 if (request['REQUEST_METHOD'] == "POST") {
 </form>
 
 <%
 if (request['REQUEST_METHOD'] == "POST") {
-       /* if they cancelled then take them back to the list of tests */
-       if (form['submit'] == "Cancel") {
-               redirect("/");
-       }
-
        function TestFunction(arg1, arg2) {
                 return "OK";
        }
        function TestFunction(arg1, arg2) {
                 return "OK";
        }
index 89e542c55291c12077354eb95850006b40a40d1f..2d3693355cc124fdcb0e8800c257197cf2dbe538 100644 (file)
@@ -1,34 +1,26 @@
 <% page_header("columns", "ESP Form Test"); %>
 
 <% page_header("columns", "ESP Form Test"); %>
 
-<form name="FormTest" method="POST" action="@@request['SCRIPT_NAME']"> 
-    firstName: <input name="firstName" type="text" value=""><br>
-    lastName:  <input name="lastName"  type="text" value=""><br>
-    <input name="submit" type="submit" value="OK">
-    <input name="submit" type="submit" value="Cancel"><br>
-</form>
+<%
+var f = Form("FormTest", 2, 2);
+f.element[0].label = "First Name";
+f.element[0].name  = "firstName";
+f.element[1].label = "Last Name";
+f.element[1].name  = "lastName";
+f.submit[0] = "OK";
+f.submit[1] = "Cancel";
+
+display_form(f);
 
 <%
   /* if its a post then the user has filled in the form, so
      report the values 
   */
 
 <%
   /* if its a post then the user has filled in the form, so
      report the values 
   */
-  if (request['REQUEST_METHOD'] == "POST") {
-
-  /* if they cancelled then take them back to the list of tests */
-  if (form['submit'] == "Cancel") {
-     redirect("/");
-  }
+  if (form['OK']) {
 %>
 %>
-  You chose firstName=@@form['firstName'] lastName=@@form['lastName']
+  You chose firstName=@@form['firstName'] lastName=@@form['lastName']<p>
+  
 <%
 <%
-
-   function showArray(name, array) {
-      write("<h3>Array: " + name + "</h3>\n");
-      for (v in array) {
-            write(name + "[" + v + "]=" + array[v] + "<br>\n");
-      }
-   }
-
-   showArray("form",    form);
+   simple_table(form);
   }
 %>
 
   }
 %>
 
index 3d787012c60f676928e56eaa99e1eca40fc203dd..ec747df4429261c56fc24338c8f065866fba32d9 100644 (file)
@@ -43,18 +43,7 @@ if (request['REQUEST_METHOD'] == "POST") {
        }
 }
 
        }
 }
 
-function showArray(name, array) {
-       write("<h3>Array: " + name + "</h3>\n");
-       if (array == undefined) {
-               write("undefined<br>\n");
-               return;
-       }
-       for (v in array) {
-               write(name + "[" + v + "]=" + array[v] + "<br>\n");
-       }
-}
-
-showArray("session", session);
+simple_table(session);
 write("SessionId=" + request['SESSION_ID'] + "<br>\n");
 %>
 
 write("SessionId=" + request['SESSION_ID'] + "<br>\n");
 %>
 
index 262a0962b7911dca6f2b5c3166357fd2b08833c9..40008a0d7d853aabf19d9b92688ce04dd5b9c532 100644 (file)
@@ -8,9 +8,7 @@
       if (array == undefined) {
         write("undefined<br>\n");
       } else {
       if (array == undefined) {
         write("undefined<br>\n");
       } else {
-            for (v in array) {
-                 write(name + "[" + v + "]=" + array[v] + "<br>\n");
-           }
+       simple_table(array);
       }
    }
 
       }
    }
 
    showArray("session", session);
 %>
 
    showArray("session", session);
 %>
 
-<form name="Cancel" method="POST" action="/"> 
-    <input name="submit" type="submit" value="Cancel"><br>
-</form>
-
 </ul>
 
 <% page_footer(); %>
 </ul>
 
 <% page_footer(); %>
index 1dfc1142e613ec1ec0bb4d0e91ab7509eb78c7c0..5d12af22584ddb47a39646a73115561671161487 100644 (file)
@@ -1,11 +1,13 @@
 <% page_header("plain", "SWAT Login"); %>
 
 <% page_header("plain", "SWAT Login"); %>
 
-<form name="login" method="POST" action="@@request.SCRIPT_NAME"> 
-    Username:   <input name="Username"  type="text" value=""><br>
-    Password:  <input name="Password" type="password" value=""><br>
-    <br>
-    <input name="submit" type="submit" value="Login"><br>
-</form>
+<%
+var f = Form("login", 2, 1);
+f.element[0].label = "Username";
+f.element[1].label = "Password";
+f.submit[0] = "Login";
+
+display_form(f);
+%>
 
 <%
        if (request.REQUEST_METHOD == "POST") {
 
 <%
        if (request.REQUEST_METHOD == "POST") {
index cc3ca55468130dc73902d18d5dc8a2f270491d06..d9e3b56bceb026e207820c6b0277765da1896824 100644 (file)
@@ -92,3 +92,60 @@ function multi_table(array, header) {
        }
        write("</table>\n");
 }
        }
        write("</table>\n");
 }
+
+/*
+  create a Form object with the defaults filled in, ready for display_form()
+ */
+function Form(name, num_elements, num_submits)
+{
+       var f = new Object();
+       f.name = name;
+       f.element = new Array(num_elements);
+       f.submit =  new Array(num_submits);
+       f.action = request.REQUEST_URI;
+       f.class = "form";
+       for (i in f.element) {
+               f.element[i] = new Object();
+               f.element[i].type = "text";
+               f.element[i].value = "";
+       }
+       return f;
+}
+
+/*
+  display a simple form from a ejs Form object
+  caller should fill in
+    f.name          = form name
+    f.action        = action to be taken on submit (optional, defaults to current page)
+    f.class         = css class (optional, defaults to 'form')
+    f.submit        = an array of submit labels
+    f.element[i].label = element label
+    f.element[i].name  = element name (defaults to label)
+    f.element[i].type  = element type
+    f.element[i].value = current value (optional, defaults to "")
+ */
+function display_form(f) {
+       write('<form name="' + f.name +
+             '" method="post" action="' + f.action + 
+             '" class="' + f.class + '">\n');
+       write("<table>\n");
+       for (i in f.element) {
+               var e = f.element[i];
+               if (e.name == undefined) {
+                       e.name = e.label;
+               }
+               if (e.value == undefined) {
+                       e.value = '""';
+               }
+               write("<tr>");
+               write("<td>" + e.label + "</td>");
+               write('<td><input name="' + e.name + '" type="' + 
+                     e.type + '" value="' + e.value + '"></td>\n');
+       }
+       write("</table>\n");
+       for (i in f.submit) {
+               write('<input name="' + f.submit[i] + 
+                     '" type="submit" value="' + f.submit[i] + '">\n');
+       }
+       write("</form>\n");
+}