spacer

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

home / programming / php_forms / 1 To page 1To page 2current pageTo page 4
[previous] [next]

Sr Instructional Designer D2L-Moodle,Clearance
WSI Nationwide, Inc.
US-NJ-Fort Monmouth

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 1

Prefilling Text Fields and Password Fields

The value in a text field (and in a password field and in a hidden field, as well) is provided in its value attribute. However, the data there must be properly encoded with htmlspecialchars() to get rid of dangerous characters such as ‘ or < or >.The code snippet in the preceding code expects the form to be submitted back to itself; thus, it extracts the current value of the text field from $_POST (it would work analogously with $_GET).

If you want to use a default value for this form element, you just have to provide this default value instead of the empty string in the PHP code:

Another possibility is to prefill form values from cookies. This is quite useful when users enter their data into a form several times. So, when they visit a form on the site a couple of days later, the old data can be retrieved from the cookie.Yes, just one cookie since only 20 cookies per domain are allowed.We use an array for that, of course. However, only strings are allowed as cookie values, so the use of serialize() and unserialize() is required.

The following function retrieves a value from the cookie that contains the form data.The order of precedence is as follows: If $_GET or $_POST contains a current value for this field, this value is used (specific versions of the function exist for $_GET and $_POST because only one of these two methods is normally used at a time). Otherwise, the script looks in $_COOKIE for an associated value. If nothing is found, an empty string is returned.

Now, prefilling the form value is easy: Because getFormDataGET() and getFormDataPOST() always return anything—including an empty string—the return value can be directly used in the text field’s value attribute.

Prefilling Multiline Text Fields

With multiline text fields, almost the same approach as with single-line text fields and password fields can be used.The only difference is the location where to put the prefill value: It belongs between <textarea> and </textarea>, as shown in the code.

Using the combined cookie/GET or cookie/POST approach, the code simplifies a bit.

home / programming / php_forms / 1 To page 1To page 2current pageTo 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 16, 2006

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