spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / php_forms2 / 1 To page 1current pageTo page 3To page 4
[previous][next]

Subject Matter Expert - Managed Services (PA)
Next Step Systems
US-PA-Wayne

Justtechjobs.com Post A Job | Post A Resume
Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs


How to Interact with Web Forms.. Part 2

Checking Mandatory Fields

When validating a form, most of the time the emphasis is on mandatory fields.You can check if they contain values in two ways:

  • Check whether the fields exist in

    $_GET/$_POST

        if (!isset($_GET[‘fieldname‘)) {
        // Error!
      }

  • Check whether the values in $_GET/$_POST contain

    information other than whitespace

      if (trim($_GET[‘fieldname‘) == ‘‘) {
        // Error!
      }

It is very important that you combine both techniques. You always have to check for a field’s existence using isset() to avoid error messages when trying to access array values that do not exist. But you always have to check whether there is something within the field apart from whitespace because text fields are always submitted. In addition, when empty, isset() always returns true independent of the field’s value.

Checking Selection Lists

When it comes to validating a selection list, the approach depends on the type of list:

  • If it is a list in which only one element may be selected, the list is considered to be filled out incorrectly if

  • No option in the list is selected

  • The selected option has an empty string as a value

  • If it is a list in which multiple elements may be selected, the list is considered to be filled out incorrectly if

  • No option in the list is selected

  • All selected options have an empty string as a value

Options with empty strings as values come into play if the list contains functionless dummy entries that have captions such as “Please choose from list.” These list options must not receive a value other than " ", so that the validation algorithm can distinguish these entries from reasonable ones.

The listing at the beginning of this phrase validates a single selection list—again including PHP code to prefill the list.

With multiple lists, a bit more work is required, as shown in the following code. If the form has been submitted, the array of selected list elements is searched. If one element that is nonempty is found, the process is complete and the user can be congratulated for the successful completion of the form. Otherwise, the form is displayed again.

home / programming / php_forms2 / 1 To page 1current pageTo page 3To page 4
[previous][next]

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint

Created: March 27, 2003
Revised: January 23, 2006

URL: http://webreference.com/programming/php_forms2/1