Previous Section  < Day Day Up >  Next Section

6.5 Displaying Default Values

Sometimes, you want to display a form with a value already in a text box or with selected checkboxes, radio buttons, or <select> menu items. Additionally, when you redisplay a form because of an error, it is helpful to preserve any information that a user has already entered. Example 6-23 shows the code to do this. It belongs at the beginning of show_form( ) and makes $defaults the array of values to use with the form elements.

Example 6-23. Building an array of defaults
if ($_POST['_submit_check']) {

    $defaults = $_POST;

} else {

    $defaults = array('delivery'  => 'yes',

                      'size'      => 'medium',

                      'main_dish' => array('taro','tripe'),

                      'sweet'     => 'cake');


If $_POST['_submit_check'] is set, that means the form has been submitted. In that case, the defaults should come from whatever the user submitted. If $_POST['_submit_check'] is not set, then you can set your own defaults. For most form parameters, the default is a string or a number. For form elements that can have more than one value, such as the multivalued <select> menu main_dish, the default value is an array.

After setting the defaults, provide the appropriate value from $defaults when printing out the HTML tag for the form element. Remember to encode the defaults with htmlentities( ) when necessary in order to prevent cross-site scripting attacks. Because of the structure of the HTML tags, you need to treat text boxes, <select> menus, text areas, and checkboxes/radio buttons differently.

For text boxes, set the value attribute of the <input> tag to the appropriate element of $defaults. Example 6-24 shows how to do this.

Example 6-24. Setting a default value in a text box
print '<input type="text" name="my_name" value="' . 

      htmlentities($defaults['my_name']). '">';

For multiline text areas, put the entity-encoded value between the <textarea> and </textarea> tags, as shown in Example 6-25.

Example 6-25. Setting a default value in a multiline text area
print '<textarea name="comments">';

print htmlentities($defaults['comments']);

print '</textarea>';

For <select> menus, add a check to the loop that prints out the <option> tags that prints a selected="selected" attribute when appropriate. Example 6-26 contains the code to do this for a single-valued <select> menu.

Example 6-26. Setting a default value in a <select> menu
$sweets = array('puff' => 'Sesame Seed Puff',

                'square' => 'Coconut Milk Gelatin Square',

                'cake' => 'Brown Sugar Cake',

                'ricemeat' => 'Sweet Rice and Meat');

print '<select name="sweet">';

// $val is the option value, $choice is what's displayed

foreach ($sweets as $option => $label) {

    print '<option value="' .$option .'"';

    if ($option =  = $defaults['sweet']) {

        print ' selected="selected"';


    print "> $label</option>\n";


print '</select>';

To set defaults for a multivalued <select> menu, you need to convert the array of defaults into an associative array in which each key is a choice that should be selected. Then, print the selected="selected" attribute for the options found in that associative array. Example 6-27 demonstrates how to do this.

Example 6-27. Setting defaults in a multivalued <select> menu
$main_dishes = array('cuke' => 'Braised Sea Cucumber',

                     'stomach' => "Sauteed Pig's Stomach",

                     'tripe' => 'Sauteed Tripe with Wine Sauce',

                     'taro' => 'Stewed Pork with Taro',

                     'giblets' => 'Baked Giblets with Salt', 

                     'abalone' => 'Abalone with Marrow and Duck Feet');

print '<select name="main_dish[  ]" multiple="multiple">';

$selected_options = array( );

foreach ($defaults['main_dish'] as $option) {

    $selected_options[$option] = true;


// print out the <option> tags

foreach ($main_dishes as $option => $label) {

    print '<option value="' . htmlentities($option) . '"';

    if ($selected_options[$option]) {

        print ' selected="selected"';


    print '>' . htmlentities($label) . '</option>';

    print "\n";


print '</select>';

For checkboxes and radio buttons, add a checked="checked" attribute to the <input> tag. The syntax for checkboxes and radio buttons is identical except for the type attribute. Example 6-28 prints a default-aware checkbox named delivery and prints three default-aware radio buttons, each named size and each with a different value.

Example 6-28. Setting defaults for checkboxes and radio buttons
print '<input type="checkbox" name="delivery" value="yes";

if ($defaults['delivery'] =  = 'yes') { print ' checked="checked"'; }

print '> Delivery?';

print '<input type="radio" name="size" value="small";

if ($defaults['size'] =  = 'small') { print ' checked="checked"'; }

print '> Small ';

print '<input type="radio" name="size" value="medium";

if ($defaults['size'] =  = 'medium') { print ' checked="checked"'; }

print '> Medium';

print '<input type="radio" name="size" value="large";

if ($defaults['size'] =  = 'large') { print ' checked="checked"'; }

print '> Large';

    Previous Section  < Day Day Up >  Next Section