PHP Form Builder Class

This project promotes rapid development of HTML forms through an object-oriented PHP framework.

Download PFBC 3.1

What's New in 3.x

Unlike the 2.x branch, version 3.x doesn't represent a complete rewrite from its previous version. In fact, most of the PHP code for creating and validating forms has remained unchanged in this new major version release. So, what's different?

The most significant enhancement is the integration with Bootstrap - a front-end framework from Twitter. Bootstrap incorporates responsive CSS, which means your forms not only look and behave great in the latest desktop browser, but in tablet and smartphone browsers as well. To see responsive CSS in action, resize your browser window and watch how the login form in the Getting Started section responds.

Another enhancement in version 3.x is the addition of 13 HTML5 elements, which you can check out in our HTML5 example. HTML5 form elements and attributes improve your form's usability - especially on tablets and smartphones where data is entered with virtual keyboards.

Here are a few more differences between PFBC 3.x and 2.x to be aware of if you're planning on upgrading.

  • In version 3.x, the Form class constructor no longer includes the width parameter.
  • PFBC 3.x contains new view classes - SideBySide, Vertical, Inline, and Search. The views used in version 2.x are no longer supported.
  • The description element property that existed in PFBC 2.x has been replaced by two properties in 3.x - shortDesc and longDesc. ShortDesc is displayed inline while longDesc is displayed block.
  • jQuery UI js/css files are no longer included by default. The js/css files will only be added when an elements that make use of jQuery UI (jQueryUIDate, Checksort, and Sort) is added to the form.
  • The Date element in PFBC 2.x has been renamed to jQueryUIDate. The new Date element in version 3.x is HTML5's input type="date".
  • The Email element now uses input type="email" as apposed to type="text".
  • The required Element property now sets the HTML5 required attribute triggering client-side validation when supported by the web browser.
  • Validation errors have been restyled using Bootstrap specific classes.

Getting Started

Before writing any code, you'll first need to download the latest version of PFBC, extract the contents of the zip file, and upload the PFBC directory within the document root of your web server. The other files/directories outside of the PFBC folder (like this one) that are included in the download are provided only for instruction and can be omitted from your production environment.

Important This project maintains two separate code bases - one for PHP 5 and another for PHP 5 >= 5.3.0. The primary difference is the use of namespaces in PFBC 3.1 (PHP 5 >= 5.3.0). Namespaces weren't introduced in PHP until version 5.3.0, so if you're web server is running an older version of PHP 5, then you will need to use PFBC 3.1 (PHP 5).

Once the PFBC directory is up on your web server, you're ready to create your first form. Below you'll find a sample login form that we'll talk through in detail.

<?php
session_start();

use PFBC\Form;
use PFBC\Element;

include("PFBC/Form.php");
$form = new Form("login");
$form->addElement(new Element\HTML('<legend>Login</legend>'));
$form->addElement(new Element\Hidden("form", "login"));
$form->addElement(new Element\Email("Email Address:", "Email", array(
    "required" => 1
)));
$form->addElement(new Element\Password("Password:", "Password", array(
    "required" => 1
)));
$form->addElement(new Element\Checkbox("", "Remember", array(
    "1" => "Remember me"
)));
$form->addElement(new Element\Button("Login"));
$form->addElement(new Element\Button("Cancel", "button", array(
    "onclick" => "history.go(-1);"
)));
$form->render();
<?php
session_start();

include("PFBC/Form.php");
$form = new Form("login");
$form->addElement(new Element_HTML('<legend>Login</legend>'));
$form->addElement(new Element_Hidden("form", "login"));
$form->addElement(new Element_Email("Email Address:", "Email", array(
    "required" => 1
)));
$form->addElement(new Element_Password("Password:", "Password", array(
    "required" => 1
)));
$form->addElement(new Element_Checkbox("", "Remember", array(
    "1" => "Remember me"
)));
$form->addElement(new Element_Button("Login"));
$form->addElement(new Element_Button("Cancel", "button", array(
    "onclick" => "history.go(-1);"
)));
$form->render();

Notice: Array to string conversion in /home/www/1001idea.com/pfbc3.1-php5.3/PFBC/Base.php on line 32

Notice: Array to string conversion in /home/www/1001idea.com/pfbc3.1-php5.3/PFBC/Base.php on line 30

Notice: Undefined property: PFBC\Element\HTML::$Array in /home/www/1001idea.com/pfbc3.1-php5.3/PFBC/Base.php on line 30

Fatal error: Uncaught Error: Function name must be a string in /home/www/1001idea.com/pfbc3.1-php5.3/PFBC/Base.php:30 Stack trace: #0 /home/www/1001idea.com/pfbc3.1-php5.3/PFBC/Element.php(25): PFBC\Base->configure(Array) #1 /home/www/1001idea.com/pfbc3.1-php5.3/PFBC/Element/HTML.php(7): PFBC\Element->__construct('', '', Array) #2 /home/www/1001idea.com/pfbc3.1-php5.3/index.php(139): PFBC\Element\HTML->__construct('<legend>Login</...') #3 {main} thrown in /home/www/1001idea.com/pfbc3.1-php5.3/PFBC/Base.php on line 30