Example using PHP Form Class

Back to Form Builder

Demonstrating common form elements output by the PHP form class.
See full source code for this example below the form.

male female

Source code generating the above form elements:

<?php
require($_SERVER['DOCUMENT_ROOT'] . 'includes/html_form.class.php');

// array for select list example
$rank_ar = array('Totally lame''Minimally useful''Pretty good''I realy like it''Fabulous');

// create form instance for this example
$frm = new HTML_Form();

// start the form 
$frmStr $frm->startForm('#''post''myForm'
    
// optional attributes for the form
    
array('class'=>'myFormClass''onsubmit'=>'return checkBeforeSubmit(this)') ) . 
    
    
// label for the text box
    
'<p>' $frm->addLabelFor('firstName''First Name: ') . 
    
    
// text box with extra attributes 
    
$frm->addInput('text''firstName''Sharon', array('id'=>'firstName''size'=>16'maxlength'=>50) ) . '</p>' 
    
    
'<p>' $frm->addLabelFor('pref''I like brownies ') . 
    
    
// checkbox added using addInput
    
$frm->addInput('checkbox''pref'1, array('id'=>'pref') ) . '</p>' 
    
    
'<p>' $frm->addLabelFor('gender''Your gender: ') . 
    
    
// radio buttons added using addInput
    
$frm->addInput('radio''gender''male', array('id'=>'gender') ) . ' male ' 
    
$frm->addInput('radio''gender''female') . ' female</p>' 
    
    
// adding a text area
    
'<p>' $frm->addTextArea('area'640'[Your comments]', array('onclick'=>'clearContents(this)') )  . '</p>' 
    
    
// adding a select list from parallel arrays
    
'<p>' $frm->addSelectListArrays('month'range(112), HTML_Form::$MONTHS_LONG''' - Month - ') . '</p>' 
    
    
'<p>' $frm->addLabelFor('rank''How would you rank this code? ') . 
    
    
// adding a select list
    
$frm->addSelectList('rank'$rank_artrue'Pretty good'null, array('id'=>'rank') ) . '</p>' 
    
    
// adding a submit button
    
'<p>' $frm->addInput('submit''submitMyForm''Submit') . '</p>' 
    
    
// end the form
    
$frm->endForm();
    
// output the collected form elements
echo $frmStr;

?>

Back to Form Builder | Top