FormBuilder: HTML forms made simple
Important: I no longer maintain FormBuilder. If you need a standalone PHP form builder I recommend Swiftmailer.
Even though HTML forms tend to be much of a muchness, each is different enough to require customized markup, styling, and validation. Sometimes it feels like reinventing the wheel. Over the past year I have been progressively streamlining the way I build my forms, and recently got to a point where I realized it wouldn’t take much more work to create an all-in-one PHP class that generates form markup, validates input, and displays error messages to the user. So that’s what I did, and I’m sharing it here in the hope someone else finds it useful too.
FormBuilder is designed to do make building forms simple. It can do pretty much everything you might want to achieve with a typical HTML form:
- Create HTML forms by entering a few PHP values
- Generates XHTML 1.0 Strict compliant markup
- Fully skinnable via CSS
- Rigorous and flexible form validation on a per-field basis
- Displays easy to understand error messages to the user
- Highlights required fields and fields that are in error
- Automatically send form results to any email address
- Displays a ‘thanks’ message once form is successfully processed
- Hide or show HTML content based on whether the form was successfully submitted
- Ability to add your own custom form validation after FormBuilder has completed its own validation pass
- Protect against PHP header injection
Demo and download
Download FormBuilder (current version is v1.5)
Important: FormBuilder is a work in progress, so I can’t promise it’s completely bug free. Feedback, suggestions and bug reports are welcomed: leave a comment below.
How the heck do I use this thing?
You should be able to get a pretty good idea how FormBuilder works just by looking at the source code of index.php in the download package. If you want to dig a little deeper I included a text file with the package that documents all aspects of the class and how to use it. And if you’re still scratching your head feel free to post a comment below.
FormBuilder must be run on a web server with PHP 4+ running (I’ve tested under both PHP4 and PHP5). A basic understanding of PHP is helpful, but if you follow the examples provided in the documentation you might be able to make do without. CSS knowledge is required if you want to change the appearance of the form.
Over at roScripts there is a nice tutorial explaining how to modify FormBuilder so that validation is performed unobtrusively using AJAX.
- Better handling of checkbox results in the
- A custom form submit URL can be passed to the FormBuilder constructor. Useful when using FormBuilder in an environment that is performing URL rewriting.
- Replaced deprecated
- Checkbox field types are correctly processed when field is not mandatory, and the user didn’t check any of the available options.
- Added new field type: file (for file uploads). Note that files are currently not emailed when using the
emailResultsmethod. Any handling of the uploaded files should be accomplished manually by accessing PHP’s
- The textbox and textarea field types now accept an optional
- Fixed a bug that meant checkboxes had a CSS class of ‘fbheckbox’ instead of ‘fbcheckbox’.
emailResultsfunction, which automatically emails the form variables to any email address
- Added new field type: password
- Added new field type: hidden
- By default there is no longer an empty
ddafter each form field, which makes FormBuilder’s markup more semantic. The empty
dd's provide a handy element for styling visual dividers between fields, so you can still reveal them using the new
showDividersparameter in the FormBuilder constructor.
usePseudoLegendsparameter. If set to true,
legendwill be replaced with
h3. The HTML
legendelement is notoriously difficult to style consistently, and this parameter provides a workable alternative.
- Cleaned up the demo CSS styles, including getting dividers displaying properly in Safari.
- Checkboxes and radio buttons are now correctly marked up inside label tags
- Labels are now associated with the relevant form field via the
Credit where credit is due
FormBuilder uses validaForms by Erick Vavretchek for form validation. Please see the validaForms PHP Classes package if you wish to learn more about this excellent from validator.