PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Mark Berube   array2form   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Illustrates aspects of building arrays for form data
Class: array2form
Generate HTML forms from input parameter lists
Author: By
Last change:
Date: 14 years ago
Size: 5,217 bytes
 

Contents

Class file image Download
<?php
include_once('class.array2form.php');
error_reporting(E_ALL);
ini_set('display_errors', 'On');
if(isset(
$_GET['which_form'])){
   
$which_form = $_GET['which_form'];
}else{
   
$which_form = 'Vendors';
}

/*Decription of array elements: Ea array element must contain a pipe-delimeted list of form input parameters:
 * <Input Type>|<Input Display Label>|<Input Name>|<Value(s)>|<input Size>|<javascript onclick>
 *
 * Input Types Recognized:
 * text: (standard text input - leave default field blank if input should be blank on page load
 * textarea: (standard textarea input - Specify columns and rows by listing 'cols,rows' in the size field
 * select: (size attribute should be '0', this class does not support multi-select dropdowns...yet. Separtate values with commas and define default selected with '(selected)' by that value. Readonly and script call should be '0' those attributes do not apply to selects)
 * radio: (size attribute should be '0', Separtate values with commas and define default checked with '(checked)' by that value. Readonly and script call should be '0' those attributes do not apply to radio buttons)
 * checkbox: (size attribute should be '0', Separtate values with commas and define default checked with '(checked)' by that value. Readonly and script call should be '0' those attributes do not apply to checkboxes)
 * date: This is a custom input, You can point the script call to a javascript calendar. The field is preset to allow 10 charecters and uses the icon to call the calendar script
 * custom: basically a text input which populates from a javascript function...similar in function to the date, the date just has a set number of charecters allowed and uses a different icon
 */
$forms = array(
   
'Vendors' => array(
                   
"text|Code:|code|011101001|15|0",
                   
"select|Invoice:||test1,test2,test3(selected),test4|0|0",//for
                   
"date|Date:|date_due||0|alert('You could open your own calendar script by specifying a javascript onclick event in the script-call placeholder')", //date types have a default size of 10 and will prefill with todays date
                   
"checkbox|Qty:|qty|1,2,3(checked),4,5|0|0",
                   
"radio|Type:|item_type|Type-1,Type-2,Type-3(checked),Type-4,Type-5|0|0",
                   
"custom|Vendor:|vend|-=Select=-|15|populate_input('vend','ACME Co.')"
                 
),
   
'Teams' => array(
                   
"text|Team:|team||15|0",
                   
"select|Class:||Class-A,Class-B(selected),Class-C,Class-D|0|0",
                   
"checkbox|Qty:|qty|1,2,3(checked),4,5|0|0",
                   
"radio|Type:|item_type|Hockey,Soccer,Baseball(checked),Basketball,Bowling|0|0",
                   
"custom|Manager:|vend|-=Select=-|15|populate_input('vend','ACME Co.')",
                   
"textarea|Comments:|comments||25,2|0"
                 
)

);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=windows-1250">

  <title>Array2Form Example</title>
  <script type="text/javascript">
  function populate_input(input_name,input_val){
      alert('By using this onclick event we will now populate the &quot;' + input_name + '&quot; input with a jvascript function');
      document.f1.vend.value = input_val;
  }
  </script>
  </head>
  <body>
<form method="post" action="somewhere.php" name="f1" enctype="multipart/form-data">
<table width=675 cellpadding="5" style="border:1px solid black;">
<tr>
    <th colspan=2>
     Now viewing form:
     <?php
    
echo $which_form;
     if(
$which_form == 'Vendors'){
         echo
"&nbsp;&nbsp;<a href=\"example.php?which_form=Teams\">(go to Teams)</a>";
     }else{
         echo
"&nbsp;&nbsp;<a href=\"example.php?which_form=Vendors\">(go to Vendors)</a>";
     }
    
    
?>
</th>
</tr>
<?php

new array2form($forms[$which_form]);

?>

<tr>
<td colspan=2 style="text-align:right;"><br><br>
    <input type="submit" value="submit" name="submit" /><input type="reset" value="reset" name="reset" />
</td>
</tr>
</table>
</form>
<div>
    Description of array2form:<br>
    <p>This class was developed for use in sites where there are a lot of forms to keep track of, and all fit within a specific template...all forms data can be kept in a single 2 dimensional array as demonstrated in the source code of this example page, using this method you can review a dozen forms data all on one screen. The forms in the example were dynamically created by passing an array of form parameters to the class. This is useful in situations where you want to build a form based on previous selections or sql queries. Click the available link to dynamically switch between forms. This example also demostrates how you can call javascript functions to integrate with your forms. </p>
    <p>The class takes an array of form-input definitions and creates table rows in a standard 2-column, vertical layout format. There is no support for other layouts yet.</p>
</div>
  </body>
</html>