|
This page is a simple introduction to one part of PEAR, HTML_QuickForm. It is by no means exhaustive; in fact, it covers a very small fraction of the total functionality of HTML_QuickForm. It is also not definitive: that role
is taken by the source code itself, which is of course always right. However, for the newcomer to PEAR or HTML_QuickForm, the following should be a useful foundation to build upon.
It is assumed that you have installed both PHP and PEAR, and that you are familiar with PHP and HTML. Help with PEAR can be found at the PEAR website; help with PHP can be found at the PHP website. It is also assumed that your PHP
scripts are correctly interpreted by your web server. The following script should display a host of information about the environment your web server runs in:
phpinfo();
If, instead, you simply see the code above displayed in your browser window then you need to resolve this problem before proceeding. There are many tutorials setting up PHP on the Internet,
and a simple search should find a suitable one.
Finally, it is a good idea to ensure that your PEAR installation is up to date. This can be done with the following command:
pear list-upgrades
# Upgrade PEAR
pear upgrade-all
Getting Started with Your first form.
We will start by creating a very simple form. Copy the following code to a file, give it a .php extension,
and display it in your browser:
require_once "HTML/QuickForm.php";
$form = new HTML_QuickForm('frmTest', 'get');
$form->addElement('header', 'MyHeader', 'Testing QuickForm');
$form->display();
Loading this file in your browser will simply show the words "Testing QuickForm" in black on a grey background. Not particularly exciting, but this does show that PEAR and
QuickForm are installed correctly and working.
Let's consider the lines individually. The next line loads the class definitions for QuickForm:
require_once "HTML/QuickForm.php";
Next we instantiate an HTML_QuickForm object:
$form = new HTML_QuickForm('frmTest', 'get');
This says that the name of the form is frmTest, and the method used to pass the form variables back to the PHP script will be GET.
By default, the form's action (the URL that will be called when the form is submitted) is the same URL that
displayed the form in the first place.
The next line creates an element on the form. An element is any item which appears on the form. It may be a text box,
a select item, plain text, etc. In this case it is a header, a psuedo-element included in QuickForm simply to improve
presentation. This element has the name MyHeader and the text value Testing QuickForm:
$form->addElement('header', 'MyHeader', 'Testing QuickForm');
Finally, the line;
$form->display();
makes it all happen. This displays the form and allows the user to interact with it. Of course,
in this example there isn't anything the user can do, so let's create a more complex example.
|