Home » HTML » HTML_Form » Manual
This is a simple HTML form generator. It supports all the HTML form element types including file uploads, may return or print the form, just individual form elements or the full form in "table mode" with a fixed layout.
Introduction
Introduction – Generating HTML forms.
Edited By
Martin JansenWhat is HTML_Form?
This is a simple HTML form generator. It supports all the HTML form element types including file uploads, may return or print the form, just individual form elements or the full form in "table mode" with a fixed layout.
Range of use
Generally one can use HTML_Form in HTML based projects of any form. But especially when using the package in "table mode" one may soon run into design problems, because the table that is created by HTML_Form cannot be equipped with CSS or other ways to control their look. Thus the main usage area of HTML_Form are sites where design does not play the most important role but where it is necessary to get fast results while keeping the code clean at the same time. (Examples are back office websites or places like pear.php.net, where the package is used at a lot of places.)
Simple usage example
Basic example
<?php
require_once "HTML/Form.php";
$form = new HTML_Form('receivingscript.php');
$form->addText("name", "What's your name?");
$form->addSubmit("submit", "Go, Go");
$form->display();
?>
The above example code creates a HTML form that only contains a single textfield with the name "name" and with the label "What's your name" and a submit button labeled "Go, Go".
Supported tags
The following HTML form tags are supported by HTML_Form: textfields, password fields, checkboxes, textareas, submit buttons, reset buttons, select fields, radio buttons, image fields, hidden fields, file upload fields. Additionally one can add blank spaces and plain text to the form.
Usage examples
Usage examples – Using HTML_Form.
Edited By
Martin JansenCreating the form in "table mode"
Creating a form in "table mode" means that the package returns the complete form in a fixed width table. Whilst this does not work very well for most frontend websites, it is a very convenient way to create forms during development phase or for backend systems where the design does not matter (much).
Creating a form
<?php
require_once "HTML/Form.php";
$form = new HTML_Form('receivingscript.php');
$form->addText("name", "What's your name?");
$form->addText("email", "What's your email address?");
$form->addPassword("password", "Please enter the desired password");
$form->addPlaintext("Tip", "Your password should be hard to guess");
$form->addSubmit("submit", "Submit");
$form->display();
?>
This will display a table where the left column holds the labels of the different fields. In the right column the different fields are placed.
Displaying single form tags
Apart from retrieving the table as a whole, one can also use the API of HTML_Form to directly display single form tags.
Directly displaying form tags
<?php
require_once "HTML/Form.php";
$form = new HTML_Form('receivingscript.php');
$form->displayText("name", "What's your name?");
?>
The above example will print the HTML markup
being necessary to render a text input field. It will not print
the surrounding <form /> tag or something
else!
Returing single form tags
Similar to displaying form tags with the display*() functions HTML_Form can also return the tag instead of printing it.
Returning form tags
<?php
require_once "HTML/Form.php";
$form = new HTML_Form('receivingscript.php');
$str = $form->returnPassword("password", "Choose a password");
?>
The variable $str now contains
HTML markup like
<input type="password" name="password" />.