Source code in QfValid.php

To see how this is used return to this tutorial index.

    // This generates a simple quickform that validates form data, both client and server side.
    // You should look at QfSimple.php before you try to understand this file.

    // Copyright (c) 2005, 2013 Parliament Hill Computers ( Author: Alain D D Williams (
    // You may used this file as the basis your own (or organisation's/company's) project (under whatever
    // licence that you see fit), but may not claim ownership or copyright of the substantially unmodified file.
    // This file is made available in the hope that it is useful, there is no warranty at all, use at your own risk.
    // SCCS: @(#)QfValid.php	1.6 10/21/13 15:08:34
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <title>Quickform Demo</title>
    <link rel="stylesheet" type="text/css" href="/DefaultStyle.css">

  <!-- A standard page header -->
  <div style='position : absolute;text-align: left;top:10px;'>
   <a href='/'><img src='/Images/kite_40_83_trans.gif' width="40" height="83" alt="Parliament Hill Computers Ltd" style="border-width: 0;"></a>
  <div style='top:0px;margin:0px auto;text-align: center; font-size:40pt; '>
   Simple Quickform
    // Stop warnings due to HTML_Quickform being stuck at PHP4
    if($ini_val = ini_get('error_reporting'))
        ini_set('error_reporting', ($ini_val & ~(E_STRICT | E_DEPRECATED)));

    // Check that the title is valid:
    function CheckTitle($t)
        global $Titles;
        return isset($Titles[$t]);

    // Check that the first name is one of an approved list, callback function used in FirstName:
    function checkFNfunc($name)
	$OKlist = array('John', 'Bill');	// A very simple approved list
	return strcmp(array_search($name, $OKlist), '');

    require_once "HTML/QuickForm.php";

    // Create a GET form with the name QfDemo:
    $form = new HTML_QuickForm('QfDemo', 'get');

    // addElement() adds a display line or an input field to the form.
    // The first argument is a standard html type (text, checkbox, hidden, ...) or a custom
    // type (header, group, ...). Other arguments depend on the type.
    $form->addElement('header', 'DemoHeader', 'Quickform Demo');

    // When the form has been filled in, trim spaces from all fields:
    $form->applyFilter('__ALL__', 'trim');

    // Add a drop down select list:
    $Titles = array('Please Select', 'Mr', 'Mrs', 'Miss', 'Dr');
    $form->addElement('select', 'Title', 'Title', $Titles);
    $form->addRule('Title', 'The title is required', 'required', null, 'client');
    $form->addRule('Title', 'The title must be in the list', 'numeric', null, 'client');
    $form->addRule('Title', 'Please select the title from the list', 'nonzero', null, 'client');
    $form->applyFilter('Title', 'CheckTitle');

    // Add a text field, the form element name is 'FirstName' with a title 'First name'
    $form->addElement('text', 'FirstName', 'First name');
    $form->addRule('FirstName', 'First name is required', 'required', null, 'client');

    // The rule checkfn is to be implemented by calling checkFNfunc(), we apply that
    // to the FirstName field - this is a server side only check
    $form->registerRule('checkfn', 'callback', 'checkFNfunc');
    $form->addRule('FirstName', 'First name is not in approved list', 'checkfn');

    // A client side check is made that LastName has been given a value.
    // All client side checks are repeated server side -- it is not safe to rely on client only checks
    $form->addElement('text', 'LastName', 'Last name');
    $form->addRule('LastName', 'Last name is required', 'required', null, 'client');

    // Age is required (must be filled in) and must be numeric:
    $form->addElement('text', 'Age', 'Age');
    $form->addRule('Age', 'Age is required', 'required');
    $form->addRule('Age', 'Age must be numeric', 'numeric', null, 'client');

    // Telephone number is checked with a regular expression, but is not required:
    $form->addElement('text', 'Telephone', 'Telephone number');
    $form->addRule('Telephone', 'The phone number must start with 0, or international ones with +',
		'regex', '/^[0+]/', 'client');

    // Standard buttons:
    $form->addElement('reset', 'Clear', 'Clear');
    $form->addElement('submit', 'Submit', 'Submit');

    // First time round this results in false, remember that, as is common in php, this
    // script is used both to generate the form and to process the results.
    if ($form->validate()) {
        # Get here when the form has been filled in and validation checks passed
        // $form->freeze();	// If we want the form redisplayed in the way that the user entered it
				// but you need to do another display()
	$res = $form->exportValues();
        // print_r($res);
	echo "Title = " . htmlspecialchars($res['Title']) . " = '" . $Titles[$res['Title']] . "'<br>\n";
	echo "FirstName = " . htmlspecialchars($res['FirstName']) . "<br>\n";
	echo "LastName = " . htmlspecialchars($res['LastName']) . "<br>\n";
	echo "Age = " . htmlspecialchars($res['Age']) . "<br>\n";
	echo "Telephone = " . htmlspecialchars($res['Telephone']) . "<br>\n";
    } else {
	$form->setDefaults(array('FirstName'=>'John', 'LastName'=>'Bloggs')); // only does something with empty form

   This is a simple form, fill in some values and press <i>Submit</i>.
   There is some validation on this form:
   <li> Firstname: client side: it must have a value; server side: the value 'Bill' or 'John'
   <li> Lastname: client side: it must have a value
   <li> Age: client side: it must have a value and be numeric
   <li> Telephone: client side: it must start '0' or '+'

  <p style="font-size: x-large; font-weight: bold;">
   Return to this <em><a href='index.php'>tutorial index</a></em>.

Return to this tutorial index.