WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : class formHTML build your HTML Forms from PHP
Categories : PHP, PHP Classes, HTML and PHP, HTML Click here to Update Your Picture
David Perez
Date : Apr 12th 2005
Grade : 4 of 5 (graded 2 times)
Viewed : 8171
File : No file for this code example.
Images : No Images for this code example.
Search : More code by David Perez
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

<?php
/*
This class will provide you a way to automate the creation
of HTML form

*/
class formHTML  //Class to generate HTML form with any king of INPUT, SELECT or TEXTAREA
{// Agregada: 09/03/2005
 
var $numFields//Number of elements in the html form
 
var $Method;     //Method to send the data
 
var $Action;     //Script that will process the data
 
var $EncType;    //Type of Data in form
 
var $Name;       //form Name
 
var $Text;       //Array of values for Input field types (SUBMIT|RESET|RADIO|CHECKBOX|FILE|TEXT|HIDDEN|IMAGE|BUTTON|PASSWORD)
 
var $TextArea;   //Array of values for Text Area Input type
 
var $List;       //Array of values for Select Input type
 
var $BrOn;       //Just if you want to add a <br> after each field
 
var $FocusField; //Name of the field that you want to be selected before page load
 
var $FieldName//Array store each field name
 
var $FieldType//Array store each Type of each field
 
var $OnSubmit;   //For JavaScript added 09/05/2005

 
function formHTML()
   {
    global
$_SERVER;                    //Needed for access the $_SERVER['PHP_SELF'] varaible
   
$this->numFields=0;                  //Setting to 0
   
$this->Method="POST";                //Setting by default to POST
   
$this->Action=$_SERVER['PHP_SELF'];  //Setting by default to $_SERVER['PHP_SELF']
   
$this->EncType="";                   //Setting by default to empty
   
$this->Name="form"  . uniqid(time());//Setting a defualt Unique form name
   
$this->BrOn=true;                    //Setting by default to true
   
$this->Text['MaxSize']=10;           //Setting by default to 10
   
$this->Text['Size']=10;              //Setting by default to 10

   
}

  function
open()
   {
//Just print the form tag
   
echo "<form name=\"" . $this->Name ."\" Method=\"" .$this->Method . "\" Action=\"" . $this->Action . "\" ENCTYPE=\"" . $this->EncType ."\"";
    if (isset(
$this->OnSubmit)) echo " OnSubmit=\"" . $this->OnSubmit . "\"";
    echo
">";
    echo
"\r"; //Just to keep the html code in order see the page generated code
   
}

  function
close()
   {
//Print
   
echo "</form>\r";
    if (isset(
$this->FocusField))
     {
      echo
"<script languaje=\"JavaScript\">\r function setFocus() \r\t{\r\t document." . $this->Name . "." .$this->FocusField . ".focus();\r\t";
      if (
$this->FindType($this->FocusField)!="SELECT")
       {
        echo
" document." . $this->Name . "." .$this->FocusField . ".select();";
       }
      echo
"\r} setFocus();\r</script>";
     }
    echo
"\r";//Just to keep the html code in order see the page generated code
   
}

  function
FindType($Field)
   {
//Find the type of any form field
   
$encontrado ="";
    for (
$x=0;$x<$this->numFields;$x++)
     {
      if (
strtoupper($Field)==strtoupper($this->FieldName[$x]))
      {
       
$encontrado = $this->FieldType[$x];
       break;
      }
     }
    return
$encontrado;
   }

  function
AddField()
 
//General Porpuse form input
  //supported types SUBMIT RESET RADIO CHECKBOX FILE TEXT HIDDEN IMAGE BUTTON PASSWORD
   
{
    if (isset(
$this->Text['Type']))
     {
       if (!((
strtoupper($this->Text['Type'])!="SUBMIT")    &&
             (
strtoupper($this->Text['Type'])!="RESET")     &&
             (
strtoupper($this->Text['Type'])!="RADIO")     &&
             (
strtoupper($this->Text['Type'])!="CHECKBOX")  &&
             (
strtoupper($this->Text['Type'])!="FILE")      &&
             (
strtoupper($this->Text['Type'])!="TEXT")      &&
             (
strtoupper($this->Text['Type'])!="HIDDEN")    &&
             (
strtoupper($this->Text['Type'])!="IMAGE")     &&
             (
strtoupper($this->Text['Type'])!="BUTTON")    &&
             (
strtoupper($this->Text['Type'])!="PASSWORD")))
         { 
// begin if block
         
echo "\t";
         if (isset(
$this->Text['Label']))
          {
           echo
$this->Text['Label'] . " ";
          }
         echo
"<input type=\"" . $this->Text['Type'] . "\" ";
         if (isset(
$this->Text['Style']))
          {
           echo
" class=\"" . $this->Text['Style'] . "\" ";
          }
         if (!isset(
$this->Text['Name']))
         
// auto asign name
         // if name is null
         
{
           
$this->Text['Name'] = "field"  . uniqid(time());
          }
         echo
" name=\"" . $this->Text['Name'] . "\" ";
         if (!isset(
$this->Text['Value']))
          {
           switch (
$this->Text['Type'])
           {
            case
"SUBMIT": $this->Text['Value']="SUBMIT";
                           break;
            case
"RESET" : $this->Text['Value']="CANCEL";
                          break;
            default      :
$this->Text['Value']="";
           }
          }
          echo
" value=\"" . $this->Text['Value'] . "\" ";
          if (isset(
$this->Text['Checked']) and $this->Text['Checked']==true)
           {
            echo 
" Checked ";
           }
          if (isset(
$this->Text['Disable']) and $this->Text['Disable']==true)
           {
            echo 
" disable=\"true\" ";
           }
          if (isset(
$this->Text['JavaEvent']) && isset($this->Text['Java']))
           {
            echo 
$this->Text['JavaEvent'] . "=\"" . $this->Text['Java'] . "\" ";
           }
          if (isset(
$this->Text['Id']))
           {
            echo
"Id=\"" . $this->Text['Id'] . "\" ";
           }
          if (isset(
$this->Text['Src']))
           {
            echo
" src=\"" . $this->Text['Src'] . "\" ";
           }
          if (isset(
$this->Text['MaxSize']))
           {
             echo
" maxlength=\"" . $this->Text['MaxSize'] . "\" ";
           }
          if (!isset(
$this->Text['Size']))  // default asign
           
{
            echo
" size=\"" . $this->Text['Size'] ."\"";
           }
          echo
" >";
          if (
$this->BrOn) echo "<br>\r";
          else echo
"\r";
         
$this->FieldType[$this->numFields]=$this->Text['Type'];
         
$this->FieldName[$this->numFields]=$this->Text['Name'];
         
$this->numFields++;
         }
//end if type
     
}
    unset(
$this->Text['Name']);
    unset(
$this->Text['Type']);
    unset(
$this->Text['Value']);
    unset(
$this->Text['Style']);
    unset(
$this->Text['Java']);
    unset(
$this->Text['JavaEvent']);
    unset(
$this->Text['Label']);
    unset(
$this->Text['MaxSize']);
    unset(
$this->Text['Size']);
    unset(
$this->Text['Checked']);
    unset(
$this->Text['Src']);
    unset(
$this->Text['Disable']);
  }
//end function

function ListOpen()
  {
   echo
"\t";
   if (!isset(
$this->List['Name'])) // auto asign name
   
{
     
$this->List['Name'] = "field"  . uniqid(time());
    }
   echo
"<select name=\"" . $this->List['Name'] . "\" ";
   if (isset(
$this->List['Multiple']) && ($this->List['Multiple']==true))
    {
     echo
" Multiple ";
    }
   if (isset(
$this->List['Size']))
    {
     echo
" size=\"" . $this->List['Size'] . "\" ";
    }
   if (isset(
$this->List['JavaEvent']) && isset($this->List['Java']))
    {
     echo 
$this->List['JavaEvent'] . "=\"" . $this->List['Java'] . "\" ";
    }
    if (isset(
$this->List['Id']))
     {
     echo
"Id=\"" . $this->List['Id'] . "\" ";
     }
   echo
">\r";
   
$this->FieldType[$this->numFields]="SELECT";
   
$this->FieldName[$this->numFields]=$this->List['Name'];
   
$this->numFields++;
   unset(
$this->List['Name']);
   unset(
$this->List['Multiple']);
   unset(
$this->List['Style']);
   unset(
$this->List['Java']);
   unset(
$this->List['JavaEvent']);
   unset(
$this->List['Label']);
   unset(
$this->List['MaxSize']);
   unset(
$this->List['Size']);
   unset(
$this->List['Checked']);
   unset(
$this->List['Src']);
   unset(
$this->List['Id']);
  }
//end function

function ListAddItem($value,$label,$selected=false) //value, label, true|false
 
{
   if (
$value!="")
    {
     echo
"\t\t<option value=\"$value\" ";
     if ((
$selected==true) || ((isset($this->List['Default'])) && ($this->List['Default']==$value)))
      {
       echo
" selected ";
      }
     echo
">";
     if (
$label!="")
      {
       echo
$label;
      }
     else
      {
       echo
$value;
      }
     echo
"</option>\r";
    }
  }
//end function

function ListClose()
  {
   echo
"\t</select>\r";
   if (
$this->BrOn) echo "<br>\r";
   else echo
"\r";
   unset(
$this->List['Default']);
  }
//end function


function AddList()
  {
   echo
"\t";
   if (!isset(
$this->List['Name'])) // auto asign name
   
{
     
$this->List['Name'] = "field"  . uniqid(time());
    }
   echo
"<select name=\"" . $this->List['Name'] . "\" ";
   if (isset(
$this->List['Multiple']) && ($this->List['Multiple']==true))
    {
     echo
" Multiple ";
    }
   if (isset(
$this->List['Size']))
    {
     echo
" size=\"" . $this->List['Size'] . "\" ";
    }
   if (isset(
$this->List['JavaEvent']) && isset($this->List['Java']))
    {
     echo 
$this->List['JavaEvent'] . "=\"" . $this->List['Java'] . "\" ";
    }
   if (isset(
$this->List['Id']))
    {
    echo
"Id=\"" . $this->List['Id'] . "\" ";
    }
   echo
">\r";
   if (isset(
$this->List['Value']))
    {
     for (
$x=0;$x<sizeof($this->List['Value']);$x++)
     {
       echo
"\t\t<option value=\"" . $this->List['Value'][$x] . "\" ";
       if (isset(
$this->List['Default'][$x]) && ($this->List['Default'][$x]==true))
        {
         echo
" selected ";
        }
       echo
">";
       if (isset(
$this->List['Label'][$x]))
        {
         echo
$this->List['Label'][$x];
        }
       else
        {
         echo
$this->List['Value'][$x];
        }
       echo
"</option>\r";
     }
    }
//and if value
   
echo "\t</select>\r";
   if (
$this->BrOn) echo "<br>\r";
   else echo
"\r";
   
$this->FieldType[$this->numFields]="SELECT";
   
$this->FieldName[$this->numFields]=$this->List['Name'];
   
$this->numFields++;
   unset(
$this->List['Name']);
   unset(
$this->List['Multiple']);
   unset(
$this->List['Default']);
   unset(
$this->List['Style']);
   unset(
$this->List['Java']);
   unset(
$this->List['JavaEvent']);
   unset(
$this->List['Label']);
   unset(
$this->List['MaxSize']);
   unset(
$this->List['Size']);
   unset(
$this->List['Checked']);
   unset(
$this->List['Src']);
   unset(
$this->List['Id']);
  }
//end function

function AddTextArea()
  {
   echo
"\t";
     
// <textarea name="textarea" cols="50" rows="5" wrap="VIRTUAL"></textarea>
   
if (!isset($this->TextArea['Name'])) // auto asign name
   
{
     
$this->TextArea['Name'] = "field"  . uniqid(time());
    }
   echo
"<textarea name=\"" . $this->TextArea['Name'] . "\" ";
   if (isset(
$this->TextArea['Cols']))
    {
     echo
"cols=\"" . $this->TextArea['Cols'] . "\" ";
    }
   if (isset(
$this->TextArea['Rows']))
    {
     echo
"rows=\"" . $this->TextArea['Rows'] . "\" ";
    }
   if (isset(
$this->TextArea['Wrap']))
    {
     echo
"wrap=\"" . $this->TextArea['Wrap'] . "\" ";
    }
   if (isset(
$this->TextArea['JavaEvent']) && isset($this->TextArea['Java']))
    {
     echo 
$this->TextArea['JavaEvent'] . "=\"" . $this->TextArea['Java'] . "\" ";
    }
   if (isset(
$this->TextArea['Id']))
    {
    echo
"Id=\"" . $this->TextArea['Id'] . "\" ";
    }
   echo
">";
   if (isset(
$this->TextArea['Value']))
    {
     echo
$this->TextArea['Value'];
    }
   echo
"</textarea>\r";
   if (
$this->BrOn) echo "<br>\r";
   else echo
"\r";
   
$this->FieldType[$this->numFields]="TEXTAREA";
   
$this->FieldName[$this->numFields]=$this->TextArea['Name'];
   
$this->numFields++;
   unset(
$this->TextArea['Name']);
   unset(
$this->TextArea['Type']);
   unset(
$this->TextArea['Value']);
   unset(
$this->TextArea['Style']);
   unset(
$this->TextArea['Java']);
   unset(
$this->TextArea['JavaEvent']);
   unset(
$this->TextArea['Label']);
   unset(
$this->TextArea['MaxSize']);
   unset(
$this->TextArea['Size']);
   unset(
$this->TextArea['Checked']);
   unset(
$this->TextArea['Src']);
   unset(
$this->TextArea['Rows']);
   unset(
$this->TextArea['Cols']);
   unset(
$this->TextArea['Wrap']);
   unset(
$this->TextArea['Id']);
  }
//end function

} // end Class


Usage Example
<?php
$forma
= new formHTML;
$forma->Name="mainForma";
$forma->Method="POST";
$forma->Action=$ACCION;
$forma->OnSubmit=$onsubmit;
$forma->open();
$forma->Text['Type']="HIDDEN";
$forma->Text['Name']="modulo";
$forma->Text['Value']=$modulo;
$forma->AddField();
$forma->Text['Type']="HIDDEN";
$forma->Text['Name']="submodulo";
$forma->Text['Value']=$submodulo;
$forma->AddField();
$forma->close();     
?>



Automatic generation of HTML code for a table. OO interface. Can define colspan, rowspan, table style, cell style, and data style. Simple, but effective.
Categories : PHP, PHP Classes, HTML, HTML and PHP
A simple class with some HTML output functions that would come in handy for consistent page layout etc.
Categories : PHP, PHP Classes, HTML and PHP, HTML, Navigation
Browser Detecor Class
Categories : PHP Classes, PHP, HTML
Display Slashdot headers on your own site
Categories : HTML and PHP, HTML, PHP
webcam cam view image ispy browser independant
Categories : Graphics, HTML, HTML and PHP, PHP
Class to build a select tag in html, useful to build select boxes from a data base
Categories : PHP, HTML and PHP, PHP Classes
How to preset a text string in a textarea input field
Categories : HTML, HTML and PHP, PHP, Beginner Guides
Vote-Poll script that has a wrapper class that allows the user to create multiple polls on the same page with little trouble.
Categories : PHP, PHP Classes, HTML and PHP
This script shows you the 7th latest php items from the mailing list archive on zend.com
Categories : HTML, HTML and PHP, HTTP, PHP
GonxTabs : Create elegant HTML tabs based interface
Categories : Navigation, HTML, HTML and PHP, PHP
Snipe.Net's Web Design Color Scheme Previewer- Allows uses to input hex codes for their text, background, and link colors, and preview the color scheme with their background image. Example: http://www.snipe.net/tech/snipeschool/hex.php3
Categories : PHP, HTML and PHP, General, Graphics, HTML
How To Create a PDF Using PHP
Categories : PHP, PDF, PHP Classes, HTML and PHP
Customizable Calendar Class
Categories : HTML and PHP, Date Time, PHP, PHP Classes, Calendar
Form is a utility class for generating html forms. It provides form initialization and regex based data validation (both server and client side) with a convenient interface. This version obsoletes version 1.0a
Categories : HTML, PHP, PHP Classes, Regexps
XTemplate, a template class for PHP
Categories : PHP Classes, HTML and PHP, PHP