C.5 Chapter 6
C.5.1 Exercise 1:
var_dump($_POST) prints:
array(4) {
["noodle"]=>
string(14) "barbecued pork"
["sweet"]=>
array(2) {
[0]=>
string(4) "puff"
[1]=>
string(8) "ricemeat"
}
["sweet_q"]=>
string(1) "4"
["submit"]=>
string(5) "Order"
}
C.5.2 Exercise 2:
function process_form( ) {
print "<ul>";
foreach ($_POST as $element => $value) {
print "<li> \$_POST[$element] = $value</li>";
}
print "</ul>";
}
C.5.3 Exercise 3:
<?php
$ops = array('+','-','*','/');
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 submitted data is valid, so process it
process_form( );
}
} else {
// The form wasn't submitted, so display
show_form( );
}
function show_form($errors = '') {
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'].'">';
// the first operand
print '<input type="text" name="operand_1" size="5" value="';
print htmlspecialchars($_POST['operand_1']) .'"/>';
// the operator
print '<select name="operator">';
foreach ($GLOBALS['ops'] as $op) {
print '<option';
if ($_POST['operator'] = = $op) { print ' selected="selected"'; }
print "> $op</option>";
}
print '</select>';
// the second operand
print '<input type="text" name="operand_2" size="5" value="';
print htmlspecialchars($_POST['operand_2']) .'"/>';
// the submit button
print '<br/><input type="submit" value="Calculate"/>';
// the hidden _submit_check variable
print '<input type="hidden" name="_submit_check" value="1"/>';
// the end of the form
print '</form>';
}
function validate_form( ) {
$errors = array( );
// operand 1 must be numeric
if (! strlen($_POST['operand_1'])) {
$errors[ ] = 'Enter a number for the first operand.';
} elseif (! strval(floatval($_POST['operand_1'])) == $_POST['operand_1']) {
$errors[ ] = "The first operand must be numeric.";
}
// operand 2 must be numeric
if (! strlen($_POST['operand_2'])) {
$errors[ ] = 'Enter a number for the second operand.';
} elseif (! strval(floatval($_POST['operand_2'])) == $_POST['operand_2']) {
$errors[ ] = "The second operand must be numeric.";
}
// the operator must be valid
if (! in_array($_POST['operator'], $GLOBALS['ops'])) {
$errors[ ] = "Please select a valid operator.";
}
return $errors;
}
function process_form( ) {
if ('+' = = $_POST['operator']) {
$total = $_POST['operand_1'] + $_POST['operand_2'];
} elseif ('-' = = $_POST['operator']) {
$total = $_POST['operand_1'] - $_POST['operand_2'];
} elseif ('*' = = $_POST['operator']) {
$total = $_POST['operand_1'] * $_POST['operand_2'];
} elseif ('/' = = $_POST['operator']) {
$total = $_POST['operand_1'] / $_POST['operand_2'];
}
print "$_POST[operand_1] $_POST[operator] $_POST[operand_2] = $total";
}
?>
C.5.4 Exercise 4:
<?php
// load the form element printing helper functions
require 'formhelpers.php';
$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 ($_POST['_submit_check']) {
// If validate_form( ) returns errors, pass them to show_form( )
if ($form_errors = validate_form( )) {
show_form($form_errors);
} else {
// The submitted data is valid, so process it
process_form( );
}
} else {
// The form wasn't submitted, so display
show_form( );
}
function show_form($errors = '') {
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>';
// Package Info
print '<tr><th colspan="2">Package</th></tr>';
print '<tr><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>Length:</td><td>';
input_text('length',$_POST);
print '</td></tr>';
print '<tr><td>Weight:</td><td>';
input_text('weight',$_POST);
print '</td></tr>';
// form end
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:
// name, street address, 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';
}
// state must be valid
if (! array_key_exists($_POST['state_1'], $GLOBALS['us_states'])) {
$errors[ ] = 'Select a valid From state';
}
// zip must be 5 digits or ZIP+4
if (!preg_match('/^\d{5}(-\d{4})?$/', $_POST['zip_1'])) {
$errors[ ] = 'Enter a valid From Zip code';
}
// second address:
// name, street address, 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';
}
// state must be valid
if (! array_key_exists($_POST['state_2'], $GLOBALS['us_states'])) {
$errors[ ] = 'Select a valid To state';
}
// zip must be 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.';
}
if ($_POST['height'] > 36) {
$errors[ ] = 'Height must be no more than 36 inches.';
}
if (! strlen($_POST['length'])) {
$errors[ ] = 'Enter a length.';
}
if ($_POST['length'] > 36) {
$errors[ ] = 'Length must be no more than 36 inches.';
}
if (! strlen($_POST['width'])) {
$errors[ ] = 'Enter a width.';
}
if ($_POST['width'] > 36) {
$errors[ ] = 'Width must be no more than 36 inches.';
}
// Weight must be <= 150
if (! strlen($_POST['weight'])) {
$errors[ ] = 'Enter a weight.';
}
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['length']) . ' x' .
htmlentities($_POST['width']) . ' x ' . htmlentities($_POST['height']);
print ' and weighs ' . htmlentities($_POST['weight']) . ' lbs.';
}
?>
C.5.5 Exercise 5:
The print_array( ) function iterates through the
array it is passed, printing out each key and value. If one of those
values is an array, then print_array( ) calls
itself, passing in the subarray to be printed. A function like
print_array( ) that invokes itself is called a
recursive function. The process_form(
) function calls print_array( ) and
tells it to print the contents of $_POST.
function print_array($ar, $prefix) {
// iterate through the array
foreach ($ar as $key => $value) {
// if the value of this element is an array, then
// call print_array( ) again to iterate over that sub-array
// and tack the key name onto the prefix
if (is_array($value)) {
print_array($value, $prefix . "['" . $key . "']");
} else {
// if the value is not an array, then print it out
// with any prefix
print $prefix;
print "['" . htmlentities($key) . "'] = ";
print htmlentities($value) . '<br/>';
}
}
}
function process_form( ) {
print_array($_POST, '$_POST');
}
|