Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: Form Builder

  1. #1
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts

    Form Builder

    Hello all, I hope this is helpful to someone

    This is a class I use to build HTML forms.

    buildForm.class.php
    PHP Code:
    <?php
    class buildForm
    {
        
    //Which standard will the form's generated code default to?
        
    var $standard "xhtml";
        
        
    //constructor        
        
    function buildForm()
        {
        }
        
        
    //Did user change standard? update it..
        
    function setStandard($value)
        {
            
    $this->standard=$value
        }
        
        
    //This creates the attribute="value" bits for each input
        
    function checkAttribute($attribute,$value)
        {
            if(!empty(
    $value))
            {
                
    $attr .= " $attribute=\"$value\"";
            }
            return 
    $attr;
        }
        
        function 
    checkOption($option)
        {
            if(!empty(
    $option))
            {
                
    $opt " $option";
            }
            return 
    $opt;
        }
        
        
    //Make a text box
        
    function makeField($type,$name,$class='',$id='',$value='',$label='',$size='',$max='',$option='',$data='',$rows='',$cols='')    
        {
            
    ##THESE CAN BE LEFT BLANK
            //pre input formatting
            
    $prefix "<label>$label</label>";
            
    //post input formatting
            
    $suffix "";
            
            
    //Make sure required fields are filled
            
    if(empty($type)||empty($name))
            {
                
    trigger_error("buildForm error: \$type and/or \$name not specified correctly!",E_USER_ERROR);
            }
            
    //Is it a select box?
            
    if($type == "select")
            {
                
    //Select Box
                //Start Tag
                
    $line "<select";
                
    //Add Attributes
                
    $line .= $this->checkAttribute('class',$class);
                
    $line .= $this->checkAttribute('id',$id);
                
    $line .= $this->checkAttribute('value',$value);
                
    $line .= $this->checkAttribute('size',$size);
                
    //Allow user to select Multiple?        
                
    $line .= $this->checkOption($option);
                
    $line .= ">\n";
                
    //Make sure it has data to fill the options with
                
    if(empty($data))
                {
                    
    trigger_error("buildForm error: \$data not specified correctly!",E_USER_ERROR);                
                }
                else
                {
                    
    //is the data in an array?
                    
    if(is_array($data))
                    {
                        
    $options array_merge($data);
                    }
                    
    //or is it in a file?
                    
    else
                    {
                        include(
    $data);
                    }
                    foreach(
    $options as $option)
                    {
                        
    $line .= "<option value=\"$option\">$option</option>\n";
                    }
                }
                
    $line .= "</select>\n";
            }
            
    //Is it a textarea?
            
    elseif($type == "textarea")
            {
                
    //Textarea
                
    $line "<textarea";
                
    $line .= $this->checkAttribute('class',$class);
                
    $line .= $this->checkAttribute('id',$id);
                
    $line .= $this->checkAttribute('rows',$rows);
                
    $line .= $this->checkAttribute('cols',$cols);
                
    $line .= $this->checkOption($option);
                
    $line .= ">";
                
    $line .= $this->checkOption($value);
                
    $line .= "</textarea>\n";
            }
            else
            {
                
    $line "<input";
                
    $line .= " type=\"$type\"";
                
    $line .= " name=\"$name\"";            
                
    //Fill in the other attributes, if they're set
                
    $line .= $this->checkAttribute('value',$value);
                
    $line .= $this->checkAttribute('size',$size);        
                
    $line .= $this->checkAttribute('maxlength',$max);        
                
    $line .= $this->checkAttribute('class',$class);
                
    $line .= $this->checkAttribute('id',$id);
                
    $line .= $this->checkOption($option);
                
    //Close the tag        
                
    if($this->standard == "xhtml")
                {
                    
    $line .= " />\n";
                }
                elseif(
    $this->standard == "html")
                {
                    
    $line .= ">\n";
                }
                else
                {
                    
    trigger_error("buildForm error: Please specify either 'xhtml' or 'html' for \$standard.",E_USER_ERROR);
                }
            }
            echo 
    $prefix,$line,$suffix;
        }
    }
    ?>
    example:
    some other PHP File:
    PHP Code:
    <?php
    include("buildForm.class.php");
    $form = new buildForm();
    //Change 'xhtml' to 'html' in order to remove slashes from the end of input tags
    $form->setstandard('xhtml');
    #makeField(type[REQ],name[REQ],class,id,value,label,size,maxlength,option,data (for SELECT elements),rows,cols,)
    #'label' sets text for the <label></label> tags.
    #'option' is for DISABLED/READONLY/CHECKED/SELECTED options as they may apply to the field. You can specify more than one by just writing your first option and then a space and writing the second
    #'data' is either an array or a path to a file with an array named '$options' which holds the data for the select box
    //create a text field
    $form->makeField('text','test','','','','Text','','','readonly');


    //Array of info for select box
    //create a select box
    $select_box_data = array('1','2','3','4');
    $form->makeField('select','select','','','','Select','','','',$select_box_data);
    //allow multiple selections
    $form->makeField('select','select','','','','Select&nbsp;Multiple','','','multiple',$select_box_data);

    //textarea
    $form->makeField('textarea','textarea','','','Hi','Textarea','','','','10','20');

    //checkboxes
    $form->makeField('checkbox','check','','','Checkbox','Checkbox','','checked');
    $form->makeField('checkbox','check','','','Checkbox 2','Checkbox 2','','');
    ?>
    This does not currently support Accesskeys, tabindex, or javascript stuff like onclick, etc. However, it would be very easy to add that support for yourself. The main reason I didn't bother with those is that I never really use them and I was trying to cut down on the flood of '', each time the function is called.

    The code DOES support XHTML or HTML standards to make it easy for your page to validate.

    (<input blah blah blah /> vs <input blah blah blah>)

    I'm sure this could be way better and there's lots of room for improvement as well as features I never thought of, so bring on the comments and critiques!

    *braces himself*

    Anyway, hope this makes someone's day
    HTH
    Dan
    Last edited by whizard; 05-26-2007 at 06:53 PM.
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #2
    Regular Coder
    Join Date
    Oct 2004
    Posts
    363
    Thanks
    0
    Thanked 18 Times in 18 Posts
    This does not currently support Accesskeys, tabindex, or javascript stuff like onclick, etc. However, it would be very easy to add that support for yourself. The main reason I didn't bother with those is that I never really use them and I was trying to cut down on the flood of '', each time the function is called.
    You could pass optional attributes as 2 element (name, value) arrays. That would allow any attributes without any "'',"s.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •