SakhaliaNetHome PageSahara TerritoryVorKutaAcceptance of cookiesAcceptance of cookies

PHP Tutorial :: Forms (VII)

PHP Example #71

Default values of a form

This example shows how to set default values for a variety of HTML controls that are very different from each other.

<?php
$sweets = array('puff' => 'Sesame Seed Puff',
'square' => 'Coconut Milk Gelatin Square',
'cake' => 'Brown Sugar Cake',
'ricemeat' => 'Sweet Rice and Meat');
$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');
if (! array_key_exists('_submit_check', $_POST)) {
$_POST['_submit_check'] = 0;
}
if ($_POST['_submit_check']) {
$defaults = $_POST;
if ($form_errors = validate_form()) {
show_form($form_errors);
} else {
process_form();
}
} else {
$defaults = array('my_name' => 'Anonymous',
'comments' => 'Something to comment about?',
'delivery' => 'yes',
'size' => 'medium',
'main_dish' => array('taro','tripe'),
'sweet' => 'cake');
show_form();
}
function process_form() {
print 'Welcome, ' . htmlentities($_POST['my_name']) . '<br/>';
}
function show_form($errors = '') {
if ($errors) {
print 'Please correct these errors: <ul><li>';
print implode('</li><li>', $errors);
print '</li></ul>';
}
// Bring the global variables to the scope of the function
global $sweets;
global $main_dishes;
global $defaults;
print '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">';
// Text field
print 'Your name:<br/>';
print '<input type="text" name="my_name"
value="' . htmlentities($defaults['my_name']) . '"/><br/>';
// Text area
print 'Your comments:<br/>';
print '<textarea name="comments" rows="4" cols="26">';
print htmlentities($defaults['comments']);
print '</textarea><br/>';
// Select menu (simple)
print 'Your order (sweets):<br/><select name="sweet">';
foreach ($sweets as $option => $label) {
print '<option value="' . htmlentities($option) . '"';
if ($option == $defaults['sweet']) {
print ' selected="selected"';
}
print "> $label</option>\n";
}
print '</select><br/>';
// Select menu (multiple)
print 'Your order (main dishes):<br/>';
print '<select name="main dish[]" multiple="multiple">';
$selected_options = array();
foreach ($defaults['main_dish'] as $option) {
$selected_options[$option] = true;
}
foreach ($main_dishes as $option => $label) {
print '<option value="' . htmlentities($option) . '"';
if (array_key_exists($option, $selected_options)) {
print ' selected="selected"';
}
print '>' . htmlentities($label) . '</option>';
print "\n";
}
print '</select><br/>';
// Checkbox
print '<input type="checkbox" name="delivery" value="yes"';
if ($defaults['delivery'] == 'yes') { print ' checked="checked"'; }
print '/> Delivery?<br/>';
// Radio buttons
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<br/>';
// Submit button
print '<input type="submit" value="Submit!"/>';
print '<input type="hidden" name="_submit_check" value="1"/>';
print '</form>';
}
function validate_form() {
$errors = array();
$_POST['my_name'] = htmlentities($_POST['my_name']);
$_POST['comments'] = htmlentities($_POST['comments']);
if (strlen($_POST['my_name']) < 3) {
$errors[] = 'Your name must have at least 3 characters long.';
}
if (strlen(trim($_POST['my_name'])) == 0) {
$errors[] = 'You must enter your name.';
}
if (strlen($_POST['comments']) < 10) {
$errors[] = 'The comment must have at least 10 characters long.';
}
if (strlen(trim($_POST['comments'])) == 0) {
$errors[] = 'You must enter a comment.';
}
return $errors;
}
?>
Your name:

Your comments:

Your order (sweets):

Your order (main dishes):

Delivery?
Small Medium Large