Version: 1.0

Type: Function

Category: HTML

License: GNU General Public License

Description: This function creates a form set in tables. It makes creating a form in a table a little less of a hassle. If you have any suggestions to make this a little better please e-mail me at formfieldsphp@kewlio.com. This is not gauranteed to be perfect.



<?php

// Examples of the creation of form fields
//
//$formFields["Name"] = array("text" => "name");
//$formFields["Password"] = array("password" => "password");
//$formFields["Radio"] = array("radio" => array("choice" => array("Yes", "No")));
//$formFields["Check"] = array("checkbox" => array("ckeckhere" => array("check1", "check2", "check3")));
//$formFields["Message"] = array("textarea" => array("message", "20", "50"));
//$formFields["Today's Date"] = array("date" => array("month", "day", "year"));
//$formFields["Retail Price"] = array("price" => "retail");
//$formFields["Select One"] = array("select" => array("pickone" => array("jan", "feb", "mar", "apr")));
//$formFields["submit"] = array("choice", "Register!");
//
//print "<form action=\"example.php\" method=\"post\">";
//form_fields($formFields);
//print "</form>";
//
//If you have any questions or comments please e-mail me at formfieldsphp@kewlio.com


function form_fields($formFields) {
   // Print beginning form table
   printf("<table border=\"0\" cellpadding=\"2\" cellspacing=\"2\">\n");

   while(list ($key1) = each ($formFields)) {
        // Print beginning row and Human Readable form field
        printf("<tr>\n");
        if($key1 == "submit") {
          printf("<td colspan=\"2\"><center><input type=\"submit\" name=\"%s\" value=\"%s\"></center></td>", $formFields["$key1"][0], $formFields["$key1"][1]);
        }
        else {
            printf("<td valign=\"top\">%s:  </td>", $key1);
        
            // Determine field type
        while(list ($key2) = each ($formFields["$key1"])) {
             if($key2 == "text" || $key2 == "password") {
               printf("<td><input type=\"$key2\" name=\"%s\"></td>\n", $formFields["$key1"]["$key2"]);
             } 
             if($key2 == "price") {
               printf("<td>$<input type=\"text\" name=\"%s\" size=\"5\" value=\"0.00\"></td>\n", $formField["$key1"]["$key2"]);
             }
             if($key2 == "radio" || $key2 == "checkbox") {
               printf("<td>\n");
               while(list ($key3) = each ($formFields["$key1"]["$key2"])) {
                    while(list ($key4, $key5) = each ($formFields["$key1"]["$key2"]["$key3"])) {
                         printf("<input type=\"$key2\" name=\"%s\" value=\"%s\"> %s<br>", $key3, $key5, $key5);
                    } 
               }
               printf("</td>\n");
             }
             if($key2 == "textarea") {
               printf("<td>\n");
               printf("<textarea name=\"%s\" rows=\"%s\" cols=\"%s\"></textarea>", $formFields["$key1"]["$key2"]["0"], $formFields["$key1"]["$key2"]["1"], $formFields["$key1"]["$key2"]["2"]);
               printf("</td>\n");
             }
             if($key2 == "date") {
               printf("<td>\n");
               printf("<select name=\"%s\">\n", $formFields["$key1"]["$key2"]["0"]);
               printf("<option value=\"jan\">January</option>");
               printf("<option value=\"feb\">February</option>");
               printf("<option value=\"mar\">March</option>");
               printf("<option value=\"apr\">April</option>");
               printf("<option value=\"may\">May</option>");
               printf("<option value=\"jun\">June</option>");
               printf("<option value=\"jul\">July</option>");
               printf("<option value=\"aug\">August</option>");
               printf("<option value=\"sep\">September</option>");
               printf("<option value=\"oct\">October</option>");
               printf("<option value=\"nov\">November</option>");
               printf("<option value=\"dec\">December</option>");
               printf("</select>\n");
               printf("<select name=\"%s\">\n", $formFields["$key1"]["$key2"]["1"]);
               for($i = 1; $i < 32; $i++) {
                  printf("<option value=\"%s\">%s</option>\n", $i, $i);
               }
               printf("</select>\n");
               printf("<select name=\"%s\">\n", $formFields["$key1"]["$key2"]["2"]);
               for($j = 1990; $j < 2021; $j++) {
                  printf("<option value=\"%s\">%s</option>\n", $j, $j);
               }
               printf("</select>\n");
               printf("</td>\n");
             }
             if($key2 == "select") {
                while(list ($key3) = each ($formFields["$key1"]["$key2"])) {
                     printf("<td><select name=\"%s\">\n", $key3);
                     while(list ($key4, $key5) = each ($formFields["$key1"]["$key2"]["$key3"])) {
                          printf("<option value=\"%s\">%s</option>\n", $key5, $key5);
                     }
                     printf("</select></td>\n");
                } 
             }
        } 
        }

        // Print ending row tag
        printf("</tr>\n");

   }

       // Print ending table tag
       printf("</table>\n"); 

}
?>