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]

Sr. Web Developer
mediabistro.com
US-NY-New York

Justtechjobs.com Post A Job | Post A Resume
Developer News
Microsoft Shows Off Silverlight 4, IE9 Plans
Metasploit Expands Vulnerability Test Framework
HyperCard Reborn?


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, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Rolling Out Your Own HTML Application Version Control · HTML 5: Client-side Storage · Working with Ajax Server Extensions
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Wi-Fi Product Watch, November 2009 · Chip Market Recovering From '08 Collapse · Low-Cost Tools to Kickstart Your New Business

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

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