Apostolos Dountsis

Web Development & Desktop Application Solutions
  • rss
  • Home
  • Projects
    • Easy Peasy Image Gallery
    • Social Bookmarks
      • Social Bookmarks Release Notes
      • Social Bookmarks Supported Sites
    • Move Comments
  • Downloads
  • Freelance Services
  • Photo Gallery
    • U2 – Vertigo Tour (Rome, Italy)
    • Brighton
    • Amsterdam
    • Athens 2004
    • Holiday Snapshots
  • Portfolio
  • About
  • Contact

Developing HTML Forms in PHP using QuickForm

Setup QuickForm

The first step in using the package would be to include it on our code. We need this package for our script to work, so we will make use of the require_once() function to include it.

require_once('HTML/QuickForm.php');

The following step is to instantiate a QuickForm object. In the constructor, we need to declare the name of the form. Additionally, the constructor can receive a number of optional arguments. More information on the constructor, can be found on the pear web site.

$form = new HTML_QuickForm(‘myform’);

Form Elements

We can add form elements by calling the addElement() function. The order in which we add elements to our form, defines the order in which the elements will be displayed on the browser.

The function receives a number of arguments. The first argument defines the type of the element. The standard HTML form types are: text, button, checkbox, hidden, submit, reset, radio, file, image, password, select and textarea. Furthermore, QuickForm supports a number of other types like date, static, header, html, link, advcheckbox and hiddenselect.

The second argument is the unique name that should be used for the element. The third argument represents the label of the element (the text that will be displayed next to the field on the browser).

Some of the element types can accept additional arguments. For example, an element of type ‘select’ can receive a fourth argument which is an array of values that would populate the items list.

Validation & Filter Rules

QuickForm has a variety of validation capabilities. These ensure that the values submitted for specific form elements meet certain predefined requirements (for example that the field is mandatory).

QuickForm makes it easy to execute these validation rules not only on the server once a form has been submitted but also via automatically-generated JavaScript in the browser before the fields are submitted.

We can specify element rules by calling the addRule() function. This function receives three arguments. The first argument is the name of the fields that the rule should be applied to. The second argument is the text that should be displayed when the rule fails. The last argument is the type of the rule that should be applied on the field. The supported rules are:

  • required: Mandatory field. The form cannot be submitted if a value is not entered.
  • maxlength: Sets the maximum length of the accepted value. This rule expects an argument for the maximum length.
  • minlength: Sets the maximum length of the accepted value. This rule expects an argument for the minimum length.
  • rangelength: The value needs to be between a minimum and maximum value (both limits needs to be specified as arguments).
  • email: Accepts only email addresses.
  • regex: The entered value needs to match a regular expression (specified as an argument).
  • lettersonly: The field accepts only letters.
  • alphanumeric: The entered value cannot be a number.
  • numeric: The value can only be a number.
  • nopunctuation: Punctuation is not allowed in the field value. The following characters are considered invalid: ( ) . / * ^ ? # ! @ $ % + = , ” ‘ > < ~ [ ] { }.
  • nonzero: 0 is not an accepted value
  • callback: Calls a function that would perform the validation.
  • compare: Compares two fields to make sure that they hold the same value. Very useful when we need to confirm a password or an email address.

The default option for the validation checks is on the server, once the form is submitted. However, QuickForm allows us to perform the checks on the client. We can achieve that by adding the string ‘client’ as a final argument on the function (if instead we use the string ‘server’ then the validation is performed on the server which is the default option). This will force QuickForm to generate JavaScript functions that will be added automatically on the generated HTML code and perform the validation checks on the client when the form is submitted.

Bookmark This Page

Add to Buzz Add to Del.icio.us Add to DotNetKicks Add to Facebook Add to Google Bookmarks Add to Mister Wong Add to Tip'd Add to Twitter Add to Yahoo My Web
Hide Sites

Pages: 1 2 3 4

Categories
PHP
Tags
PEAR, PHP
Comments rss
Comments rss
Trackback
Trackback

« A Solid Block of Code is Like a Well Proven Theorem EPIG Add-Ons »

2 Responses to “Developing HTML Forms in PHP using QuickForm”

  1. Trent says:
    June 27, 2006 at 12:35

    Very concise. Thanks, I’ll be using this info!

  2. PEAR QuickForm - Formular mit Validierung (PHP) | SteviesWebsite Blog says:
    May 21, 2009 at 14:40

    [...] Developing HTML Forms in PHP using QuickForm (dountsis.com, en) [...]

Leave a Reply

Click here to cancel reply.

About Dountsis.com

Dountsis.com is the site of the web developer, technology enthusiast, and gadgets addict, Apostolos Dountsis. The articles on this site focus on web development, software and freelance.

Topics

  • EPIG (2)
  • Firefox (6)
  • Funny (1)
  • Programming (3)
  • Site Updates (7)
  • Tutorials (4)
    • .NET (1)
    • PHP (3)
  • Windows (5)
  • WordPress (25)

PayPal Button

If you feel that the open-source projects that I develop and deliver through the site are worth a penny then you could press the button below to send it to me.

Keywords

.NET adblock Bad Sectors C# chkdsk Del.icio.us Eclipse Extension Firefox Hard Disk Microsoft Move Comments PEAR PHP Social Bookmarks Spam User Control Windows Windows XP WinForm WordPress

What I'm Doing...

  • Nite nite 1 week ago
  • RT @JulieMaggot: Τζουλια ΕΕΕΛΑΑΑΑΑ!, Θα κανουμε Σαμπανιαααααααααααα!! #julia_porn 1 week ago
  • My back! Ouch ouch 3 weeks ago
  • More updates...

Recent Articles

  • Social Bookmarks 4.1.2 Point Release
  • How to use User Controls to create MDI WinForm Applications
  • Social Bookmarks Sites Cleanup
  • Social Bookmarks 4.1.1
  • WordPress Plugins Activation Order
  • WordPress Plugin Development

Spam Blocked

65,105 spam comments
blocked by
Akismet
rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox