SakhaliaNetHome PageSahara TerritoryVorKutaAcceptance of cookiesAcceptance of cookies

PHP Tutorial :: Exercises (VI)

PHP Exercise #14

Postal form

Write a program that shows, validate and process a form to provide information about the sending of a package. The form should contain data input for the addresses of both the sender and the addressee, the size of the package and its weight. Validation should check that the package does not weight more than 150 pounds and that no measure in the package exceeds 36 inches. You can assume that provided addresses are all from United States, but you should check that the state and ZIP code provided are valid with a correct syntax. The processing function should print the information about the package in an organized and formatted report.

<?php
// The auxiliary functions are included in the file formhelpers.php
// that is invoked by require
// Prints a text field
function input_text($element_name, $values) {
print '<input type="text" name="' . $element_name . '" value="';
print htmlentities($values[$element_name]) . '"/>';
}
// Prints a submit button
function input_submit($element_name, $label) {
print '<input type="submit" name="' . $element_name . '" value="';
print htmlentities($label) . '"/>';
}
// Prints a textarea
function input_textarea($element_name, $values) {
print '<textarea name="' . $element_name . '">';
print htmlentities($values[$element_name]) . '</textarea>';
}
// Prints a radio button or a 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"';
}
print '/>';
}
// Prints a select menu
function input_select($element_name, $selected, $options, $multiple = false) {
// Prints the select label
print '<select name="' . $element_name;
// If multiple options are allowed, adds the multiple attribute
// and adds [] to the end of the label's name
if ($multiple) { print '[]" multiple="multiple'; }
print '">';
// Sets the list of selectable elements
$selected_options = array();
if ($multiple) {
foreach ($selected[$element_name] as $val) {
$selected_options[$val] = true;
}
} else {
$selected_options[$selected[$element_name]] = true;
}
// Prints the option labels
foreach ($options as $option => $label) {
print '<option value="' . htmlentities($option) . '"';
if (array_key_exists($option, $selected_options)) {
if ($selected_options[$option]) {
print ' selected="selected"';
}
}
print '>' . htmlentities($label) . '</option>';
}
print '</select>';
} // Here ends the content of formhelpers.php
// Loads the auxiliary functions for printing the form
require 'scripts/formhelpers.php';
// Array containing all the US states
$us_states = array('AL' => 'Alabama', 'AK' => 'Alaska', 'AZ' => 'Arizona',
'AR' => 'Arkansas', 'CA' => 'California', 'CO' => 'Colorado',
'CT' => 'Connecticut', 'DE' => 'Delaware', 'FL' => 'Florida',
'GA' => 'Georgia', 'HI' => 'Hawaii', 'ID' => 'Idaho', 'IL' => 'Illinois',
'IN' => 'Indiana', 'IA' => 'Iowa', 'KS' => 'Kansas', 'KY' => 'Kentucky',
'LA' => 'Louisiana', 'ME' => 'Maine', 'MD' => 'Maryland',
'MA' => 'Massachusetts', 'MI' => 'Michigan', 'MN' => 'Minnesota',
'MS' => 'Mississippi', 'MO' => 'Missouri', 'MT' => 'Montana',
'NE' => 'Nebraska', 'NV' => 'Nevada', 'NH' => 'New Hampshire',
'NJ' => 'New Jersey', 'NM' => 'New Mexico', 'NY' => 'New York',
'NC' => 'North Carolina', 'ND' => 'North Dakota', 'OH' => 'Ohio',
'OK' => 'Oklahoma', 'OR' => 'Oregon', 'PA' => 'Pennsylvania',
'RI' => 'Rhode Island', 'SC' => 'South Carolina', 'SD' => 'South Dakota',
'TN' => 'Tennessee', 'TX' => 'Texas', 'UT' => 'Utah', 'VT' => 'Vermont',
'VA' => 'Virginia', 'WA' => 'Washington', 'DC' => 'Washington D.C.',
'WV' => 'West Virginia', 'WI' => 'Wisconsin', 'WY' => 'Wyoming');
if (! array_key_exists('_submit_check', $_POST)) {
$_POST['_submit_check'] = 0;
}
if ($_POST['_submit_check']) {
// If validate_form() returns errors, pass them to show_form()
if ($form_errors = validate_form()) {
show_form($form_errors);
} else {
// The data sent is valid, then process it
process_form();
}
} else {
// The form was not sent, then show it
show_form();
}
function show_form($errors = '') {
// Declaration of all the elements of $_POST[]
$_POST['name_1'] = ''; $_POST['address_1'] = ''; $_POST['city_1'] = '';
$_POST['state_1'] = ''; $_POST['zip_1'] = ''; $_POST['name_2'] = '';
$_POST['address_2'] = ''; $_POST['city_2'] = ''; $_POST['state_2'] = '';
$_POST['zip_2'] = ''; $_POST['height'] = ''; $_POST['width'] = '';
$_POST['lenght'] = ''; $_POST['weight'] = '';
if ($errors) {
Print 'You need to correct the following errors: <ul><li>';
print implode('</li><li>',$errors);
print '</li></ul>';
}
// The beginning of the form
print '<form method="post" action="' . $_SERVER['PHP_SELF'] . '">';
print '<table>';
// The first address
print '<tr><th colspan="2">From</th></tr>';
print '<td>Name:</td><td>';
input_text('name_1', $_POST);
print '</td></tr>';
print '<tr><td>Street Address:</td><td>';
input_text('address_1', $_POST);
print '</td></tr>';
print '<tr><td>City, State, ZIP:</td><td>';
input_text('city_1', $_POST);
print ', ';
input_select('state_1', $_POST, $GLOBALS['us_states']);
input_text('zip_1', $_POST);
print '</td></tr>';
// The second address
print '<tr><th colspan="2">To</th></tr>';
print '<td>Name:</td><td>';
input_text('name_2', $_POST);
print '</td></tr>';
print '<tr><td>Street Address:</td><td>';
input_text('address_2', $_POST);
print '</td></tr>';
print '<tr><td>City, State, ZIP:</td><td>';
input_text('city_2', $_POST);
print ', ';
input_select('state_2', $_POST, $GLOBALS['us_states']);
input_text('zip_2', $_POST);
print '</td></tr>';
// Information about the package
print '<tr><th colspan="2">Package</th></tr>';
print '<td>Height:</td><td>';
input_text('height', $_POST);
print '</td></tr>';
print '<tr><td>Width:</td><td>';
input_text('width', $_POST);
print '</td></tr>';
print '<tr><td>Lenght:</td><td>';
input_text('lenght', $_POST);
print '</td></tr>';
print '<tr><td>Weight:</td><td>';
input_text('weight', $_POST);
print '</td></tr>';
// End of the form
print '<tr><td colspan="2"><input type="submit" value="Ship Package">
</td></tr>';
print '</table>';
print '<input type="hidden" name="_submit_check" value="1"/>';
print '</form>';
}
function validate_form() {
$errors = array();
// First address:
// The name, street and city are all required
if (! strlen(trim($_POST['name_1']))) {
$errors[] = 'Enter a From name.';
}
if (! strlen(trim($_POST['address_1']))) {
$errors[] = 'Enter a From street address.';
}
if (! strlen(trim($_POST['city_1']))) {
$errors[] = 'Enter a From city.';
}
// The state must be valid
if (! array_key_exists($_POST['state_1'], $GLOBALS['us_states'])) {
$errors[] = 'Select a valid From state.';
}
// The ZIP code must have 5 digits or ZIP+4
if (! preg_match('/^\d{5}(-\d{4})?$/', $_POST['zip_1'])) {
$errors[] = 'Enter a valid From ZIP code.';
}
// Second address:
// The name, street and city are all required
if (! strlen(trim($_POST['name_2']))) {
$errors[] = 'Enter a To name.';
}
if (! strlen(trim($_POST['address_2']))) {
$errors[] = 'Enter a To street address.';
}
if (! strlen(trim($_POST['city_2']))) {
$errors[] = 'Enter a To city.';
}
// The state must be valid
if (! array_key_exists($_POST['state_2'], $GLOBALS['us_states'])) {
$errors[] = 'Select a valid To state.';
}
// The ZIP code must have 5 digits or ZIP+4
if (! preg_match('/^\d{5}(-\d{4})?$/', $_POST['zip_2'])) {
$errors[] = 'Enter a valid To ZIP code.';
}
// Package
// Each dimension must be <= 36
if (! strlen($_POST['height'])) {
$errors[] = 'Enter a height.';
} elseif (! floatval($_POST['height']) == $_POST['height']) {
$errors[] = 'Height must be a numeric value.';
}
if ($_POST['height'] > 36 ) {
$errors[] = 'Height must be no more than 36 inches.';
}
if (! strlen($_POST['width'])) {
$errors[] = 'Enter a width.';
} elseif (! floatval($_POST['width']) == $_POST['width']) {
$errors[] = 'Width must be a numeric value.';
}
if ($_POST['width'] > 36 ) {
$errors[] = 'Width must be no more than 36 inches.';
}
if (! strlen($_POST['lenght'])) {
$errors[] = 'Enter a lenght.';
} elseif (! floatval($_POST['lenght']) == $_POST['lenght']) {
$errors[] = 'Lenght must be a numeric value.';
}
if ($_POST['lenght'] > 36 ) {
$errors[] = 'Lenght must be no more than 36 inches.';
}
// The weight must be <= 150
if (! strlen($_POST['weight'])) {
$errors[] = 'Enter a weight.';
} elseif (! floatval($_POST['weight']) == $_POST['weight']) {
$errors[] = 'Weight must be a numeric value.';
}
if ($_POST['weight'] > 150 ) {
$errors[] = 'Weight must be no more than 150 pounds.';
}
return $errors;
}
function process_form() {
print 'The package is going from: <br/>';
print htmlentities($_POST['name_1']) . '<br/>';
print htmlentities($_POST['address_1']) . '<br/>';
print htmlentities($_POST['city_1']) . ', ' . $_POST['state_1'] . ' '
. $_POST['zip_1'] . '<br/>';
print 'The package is going to: <br/>';
print htmlentities($_POST['name_2']) . '<br/>';
print htmlentities($_POST['address_2']) . '<br/>';
print htmlentities($_POST['city_2']) . ', ' . $_POST['state_2'] . ' '
. $_POST['zip_2'] . '<br/>';
print 'The package is ' . htmlentities($_POST['lenght']) . ' x ' .
htmlentities($_POST['width']) . ' x ' . htmlentities($_POST['height']);
print ' and weighs ' . htmlentities($_POST['weight']) . ' lbs.';
}
?>
From
Name:
Street Address:
City, State, ZIP:,
To
Name:
Street Address:
City, State, ZIP:,
Package
Height:
Width:
Lenght:
Weight: