Download

The files misc.php, DBF_Form.class, TemplateHelper.class, form-styling.css and form-behaviour.js can be found in the subversion repository.

Example Usage

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
            "http://www.w3.org/TR/html4/strict.dtd">
<title>forms test</title>
<link rel="stylesheet" href="form-styling.css" type="text/css">
<script src="form-behaviour.js" type="text/javascript"></script>

<?php

/* Require the DBF_Form and related classes to be defined. */
require_once 'DBF_Form.class';

/* Require the TemplateHelper class to be defined, for HTML output. */
require_once 'TemplateHelper.class';

/* Create an HTML 4.01 Strict template helper. */
$site = (object)array('flavour' => 'html_401_strict');
$th = new TemplateHelper($site);

/* This function we'll use later. It basically just makes sure a string doesn't
 * contain 'FOO'.
 */
function blahblah ($x)
{
    if (preg_match('/FOO/', $x))
        return 0;
    return 2;
}

/* Somewhere to keep our fields. */
$t = array();

/* Create some form fields:
 *
 *     - A text input for an e-mail address
 *     - A drop-down selection of language
 *     - Checkbox to join mailing list
 *     - Textarea for description
 *     - Another checkbox
 *     - A text box asking for a number
 *     - A text box asking for a floating point number
 */
$t[1] = new DBF_Field_Text('test', '', 'E-mail Address (no Hotmail)');
$t[2] = new DBF_Field_Selection('choose', 'en', 'Preferred Language');
$t[3] = new DBF_Field_Boolean('yesno', FALSE, 'Join Mailing List?');
$t[4] = new DBF_Field_Memo('test_long', "describe\nyourself\nhere", 'About You');
$t[5] = new DBF_Field_Boolean('yesno2', TRUE, 'Do you like cheese?');
$t[6] = new DBF_Field_Text('myint', '123', 'Favourite Number');
$t[7] = new DBF_Field_Text('myfloat', '123.45', 'Favourite Number with Decimal Point',
        'You may use either a full stop or a comma as a decimal point.');

/* Server-side validity check. TRUE flag indicates this should be eval()ed as 
 * PHP code. Check that it doesn't contain the string 'hotmail'.
 */
$t[1]->set_validity_check("\$server_status=preg_match('/hotmail/', \$val)?0:2;", TRUE);

/* Client-side check on same field. Check it is a valid e-mail address. */
$t[1]->check_validity_client(DBF_CV_IS_EMAIL);

/* Add possible options to the selection. */
$t[2]->add_option('de', 'German');
$t[2]->add_option('en', 'English');
$t[2]->add_option('fr', 'French');
$t[2]->add_option('jp', 'Japan');

/* Another server-side check. Note that instead of passing PHP code as a string
 * we can call a particular function.
 */
$t[4]->set_validity_check('blahblah', FALSE);

/* A few more client-side checks. */
$t[4]->check_validity_client(DBF_CV_UPPERCASE);
$t[6]->check_validity_client(DBF_CV_IS_INTEGER);
$t[7]->check_validity_client(DBF_CV_IS_NUMERIC);

/* Assemble these into a form with three tabs. Form should send results to URL
 * '#' using HTTP GET.
 */
$form = new DBF_Form('#', 'get');
$form->new_tab('Contact Details');
$form->add_field($t[1]);
$form->add_field($t[2]);
$form->add_field($t[3]);
$form->new_tab('Description');
$form->add_field($t[4]);
$form->add_field($t[5]);
$form->new_tab('Numbers');
$form->add_field($t[6]);
$form->add_field($t[7]);

/* Accept the submission and validate. */
$ok = $form->accept_submission() && $form->validate();

/* If valid, print results. */
if ($ok)
{
    $vals = $form->get_values();
    print "<h1>Form Submission OK.</h1>\n";
    print "<pre>";
    print htmlentities(print_r($vals, TRUE));
    print "</pre>";
}

/* Otherwise print form. */
else
{
    print $form->draw_container($th, FALSE);
}

?>

Result

Rendering of form above.
Project hosted by SourceForge.net.
SourceForge.net