Go Back   CodingForums.com > :: Server side development > PHP > Post a PHP snippet

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-26-2007, 06:47 PM   PM User | #1
whizard
Senior Coder

 
whizard's Avatar
 
Join Date: Jan 2005
Location: Philadelphia, PA, USA
Posts: 1,457
Thanks: 10
Thanked 37 Times in 37 Posts
whizard will become famous soon enoughwhizard will become famous soon enough
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
__________________
If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.
Step 1: Learn. Step 2: Search. Step 3: Post here.

Last edited by whizard; 05-26-2007 at 06:53 PM..
whizard is offline   Reply With Quote
Old 06-10-2007, 08:20 PM   PM User | #2
schleppel
Regular Coder

 
Join Date: Oct 2004
Posts: 330
Thanks: 0
Thanked 13 Times in 13 Posts
schleppel is an unknown quantity at this point
Quote:
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.
schleppel is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:21 PM.


Advertisement
Log in to turn off these ads.