Handling HTML Forms with PHP

 

Creating a form in PhpED

One of the most common uses of PHP is for form handling i.e. processing the data submitted by users through HTML forms. Whether developing a search engine, a contact form, or blogging software, handling an HTML form with PHP is a simple, four-step process:

  1. Create the HTML form the submits data to a PHP page (the figure shows PhpED’s form building tool).
  2. Create the PHP page that receives the data.
  3. Have PHP validate the data and report any errors.
  4. Have PHP do whatever with the data—use it in an email, run it in a database query, etc.—with the data.

The two most important considerations for the HTML form are its action and method attributes. The form’s action specifies which PHP page should receive the data. The form’s method dictates whether the data should be accessed by the $_GET or the $_POST superglobal array. From that point forward, what you do with the data depends upon the needs of the application. This is where the other topics and technologies in this PHP tutorial come into play.

PHP Functions

PhpED showing a function's syntax

PHP has hundreds upon hundreds of built-in PHP functions. Hundreds more are accessible by enabling support for various extensions in your PHP installation. In order to use any PHP function, one has to understand the PHP manual’s syntax for describing a function. The syntax looks like so: return_type function_name (argument_types, [optional_arguments]); The return_type and argument_typesgenerally correspond to the supported PHP data structures, with two exceptions. A return_type of void indicates that the function returns nothing (see the exit() function for an example). A type of mixed means that multiple kinds of data can be supplied as that argument or will be returned by that function (for example, the isset() function can take any type as an argument). Function arguments placed within square brackets are optional. These arguments are always listed last. Because invoking PHP functions is such a large part of PHP programming, IDE’s, likeNuSphere’s PhpED, provide a clear and vast improvement over any text editor in this regard. NuSphere’s PhpED will complete function definitions for you, helping to avoid careless spelling errors, and show a function’s exact syntax, saving you trips back to the PHP manual (see the figure). You can download the free trial to see this for yourself. PHP also supports the ability to define and use your own functions. As with PHP’s built-in functions, an IDE like NuSphere’s PhpED will aid you in calling your own defined functions. Understanding how to define and use your own function can also be seen as a baby step towards Object-Oriented PHP programming.

PHP Strings and Regular Expressions

One of the eight data structures supported in PHP is the string. A PHP string is any quoted value, from a single letter (PHP does not differentiate between strings and characters, for the most part) to combinations of letters, numbers, and punctuation. PHP has dozens of built-in functions expressly for the purpose of manipulating strings, including those for applying regular expressions to PHP strings. Regular expressions (in any programming language) allow for matches to be made based upon patterns, not literal values. A popular use of regular expressions is to match an email address, which must abide by very specific guidelines. PHP supports two types of regular expressions: POSIX and PCRE (Perl-compatible regular expression). Each type is used for the same purpose, but they differ in their syntax, and the PHP functions used to apply them to strings. Because using regular expressions requires mastering a complex set of syntactic rules, they are not for the faint of heart, but as PHP strings and regular expressions are so common in Web-based applications, the subjects are well worth the effort.

Advertisements