Creating webforms quickly

I from time to time want to create a quick static web form to run where PHP etc isn't involved. The best way I've found to do this is with the HTML_QuickForm PEAR package.

To install it on Debian, just install the php-pear package and run sudo pear install HTML_Common HTML_QuickForm and then you can easily create really nifty forms.

For example, the comments form below was created with the following input.

<?php

require_once 'HTML/QuickForm.php';

$form = new HTML_QuickForm('comments_form');

$form->addElement('header', null, 'Add a comment');

$form->addElement('text', 'author', 'Name', array('size' => 50, 'maxlength' => 50));
$form->addRule('author', 'Please enter a name', 'required', null, 'client');

$form->addElement('text', 'email', 'Email', array('size' => 50, 'maxlength' => 50));
$form->addRule('email', 'Please enter an email', 'required', null, 'client');

$form->addElement('text', 'url', 'Website', array('size' => 50, 'maxlength' => 50));

$form->addElement('textarea', 'body', 'Comment:', array('rows'=>10,'cols'=>50));
$form->addRule('body', 'Please enter a comment', 'required', null, 'client');

$form->addElement('submit', null, 'Submit');

$form->display();

?>

It automagically does client side javascript to do first level validation and makes the form look quite nice. You can just run php file.php on the command line to scrape out the plain HTML.