O'Reilly logo

Learning PHP 5 by David Sklar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Putting It All Together

Turning the humble web form into a feature-packed application with data validation, printing default values, and processing the submitted results might seem like an intimidating task. To ease your burden, this section contains a complete example of a program that does it all:

  • Displaying a form, including default values

  • Validating the submitted data

  • Redisplaying the form with error messages and preserved user input if the submitted data isn't valid

  • Processing the submitted data if it is valid

The do-it-all example relies on some helper functions to simplify form element display. These are listed in Example 6-29.

Example 6-29. Form element display helper functions

//print a text box function input_text($element_name, $values) { print '<input type="text" name="' . $element_name .'" value="'; print htmlentities($values[$element_name]) . '"/>'; } //print a submit button function input_submit($element_name, $label) { print '<input type="submit" name="' . $element_name .'" value="'; print htmlentities($label) .'"/>'; } //print a textarea function input_textarea($element_name, $values) { print '<textarea name="' . $element_name .'">'; print htmlentities($values[$element_name]) . '</textarea>'; } //print a radio button or checkbox function input_radiocheck($type, $element_name, $values, $element_value) { print '<input type="' . $type . '" name="' . $element_name .'" value="' . $element_ value . '" '; if ($element_value = = $values[$element_name]) { print ' checked="checked"'; ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required