Exchanging Information with Users: Displaying Default Values in PHP

Sometimes, you want to display a form with a value already in a text box or with pre­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 7-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 7-23. Building an array of defaults

if ($_SERVER[‘REQUEST_METHOD’] == ‘POST’) {

$defaults = $_POST;

} else {

$defaults = array(‘delivery’ => ‘yes’,

‘size’ => ‘medium’,

‘main_dish’ => array(‘taro’,’tripe’),

‘sweet’ => ‘cake’);

}

If $_SERVER[‘REQUEST_METHOD’] is POST, that means the form has been submitted. In that case, the defaults should come from whatever the user submitted. Otherwise, you can set your own defaults. For most form parameters, the default is a string or a num­ber. 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 print­ing 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 7-24 shows how to do this.

Example 7-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 7-25.

Example 7-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 so that it prints a selected attribute when appropriate. Example 7-26 contains the code to do this for a single-valued <select> menu.

Example 7-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”>’;

// > is the option value, $label is what’s displayed

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

print ‘<option value=”‘ .$option .'”‘;

if ($option == $defaults[‘sweet’]) {

print ‘ 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 attribute for the options found in that associative array. Example 7-27 demonstrates how to do this.

Example 7-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>’;

$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 (array_key_exists($option, $selected_options)) {

print ‘ selected’;

}

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

print “\n”;

}

print ‘</select>’;

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

Example 7-28. Setting defaults for checkboxes and radio buttons

print ‘<input type=”checkbox” name=”delivery” value=”yes”‘;

if ($defaults[‘delivery’] == ‘yes’) { print ‘ checked’; }

print ‘> Delivery?’;

$checkbox_options = array(‘small’ => ‘Small’,

   ‘medium’ => ‘Medium’,

   ‘large’ => ‘Large’);

foreach ($checkbox_options as $value => $label) {

print ‘<input type=”radio” name=”size” value=”‘.$value.'”‘;

if ($defaults[‘size’] == $value) { print ‘ checked’; }

print “> $label “;

}

Source: Sklar David (2016), Learning PHP: A Gentle Introduction to the Web’s Most Popular Language, O’Reilly Media; 1st edition.

Leave a Reply

Your email address will not be published. Required fields are marked *