Developing HTML Forms in PHP using QuickForm
A Working Example
The following example displays a contact form, that require a name and a message to be entered. The email address is an optional field, but if the user decides to submit one then we make sure that we receive an email address in the correct format (xxxx@xxxx.xxx).
require('HTML/QuickForm.php');
// general purpose library functions
include_once('common.php');
$form =& new HTML_QuickForm(‘contact’);
// The form elements
$form->addElement(‘html’, ‘Use the form below to contact me.’);
$form->addElement(‘header’, ‘contact_header’, ‘Contact Form’);
$form->addElement(‘text’, ‘contact_name’, ‘Your name:’);
$form->addElement(‘text’, ‘contact_email’, ‘Your e-mail address:’);
$form->addElement(‘textarea’, ‘contact_body’, ‘Your message:’);
$form->addElement(’submit’, ’submit_button’, ‘Send’);
// Validation rules
$form->addRule(‘contact_email’,
‘Please specify a valid email address’, ‘email’);
$form->addRule(‘contact_name’,
‘Please enter your name’, ‘required’);
$form->addRule(‘contact_body’,
‘Please enter some text’, ‘required’);
// Display the form if validation fails
// otherwise call on_validate()
if(!$form->validate())
{
$form->display();
}
else
{
$form->process(‘on_validate’);
}
// The function the processes the submitted values
function on_validate($data)
{
print(‘You have entered the following:’);
pre_print_r($data);
}
?>
In the next article, we will be concentrating on how we can make use of QuickForm to upload a file and how we can group elements together like radio boxes.

(1 votes, average: 4.00 out of 5)














Very concise. Thanks, I’ll be using this info!
[...] Developing HTML Forms in PHP using QuickForm (dountsis.com, en) [...]