Hi i need to combine these two files and make form sticky as in keeping values o
ID: 3605492 • Letter: H
Question
Hi i need to combine these two files and make form sticky as in keeping values of previous entry after an error. The book is no help so im in need of help here.
php file
<?php
$coffeeNames['bv'] = 'Boca Villa';
$coffeeNames['sbr'] = 'South Beach Rhythm';
$coffeeNames['pp'] = 'Pumpkin Paradise';
$coffeeNames['ss'] = 'Sumatran Sunset';
$coffeeNames['bb'] = 'Bali Batur';
$coffeeNames['dd'] = 'Double Dark';
//Coffee prices array
$coffeeCosts['bv'] = 7.99;
$coffeeCosts['sbr'] = 8.99;
$coffeeCosts['pp'] = 8.99;
$coffeeCosts['ss'] = 9.99;
$coffeeCosts['bb'] = 10.95;
$coffeeCosts['dd'] = 9.95;
// Information has been posted, check and validate form items
//
// Check which coffee was picked
$coffeeCode = $_POST['coffeeCode'];
if (!isset($coffeeNames[$coffeeCode])) {
$errors[] = 'Please select a coffee to be purchased.';
}
//
// Check for regular/decaf
if (isset($_POST['coffeeType'])) {
$coffeeType = $_POST['coffeeType'];
}
else {
$coffeeType = NULL;
}
if ($coffeeType == "caf") {
$coffeeType = 'Regular';
}
elseif ($coffeeType == "decaf") {
$coffeeType = 'Decaffeinated';
}
else {
$errors[] = 'Please select regular or decaffeinated.';
}
//
// Check coffee quantity
// NOTE: There is more error checking that is required here
$quantity = $_POST['quantity'];
if (!is_numeric($quantity)) {
$errors[] = 'Please enter a numeric value for quantity.';
}
elseif ($quantity < 0.5) {
$errors[] = 'Please enter 0.5 pounds or greater for quantity.';
}
elseif (($quantity * 10) - intval($quantity * 10)) {
$errors[] = 'Please enter quantity in 0.5 pound increments.';
}
elseif ($quantity > 10) {
$errors[] = 'Please enter a quantity of 10 pounds or less.';
}
//
// Check order name
$name = trim($_POST['name']);
if (empty($name)) {
$errors[] = 'Please enter a name.';
}
//
// Check order email
// NOTE: There is more error checking than required for the email address
$email = trim($_POST['email']);
if (empty($email)) {
$errors[] = 'Please enter an e-mail address.';
}
else {
// Check to see if entered e-mail is valid
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = 'Please enter a valid e-mail address.';
}
}
//
// Check order phone
$phone = trim($_POST['phone']);
if (empty($phone)) {
$errors[] = 'Please enter a phone number.';
}
//
// Check various parts of order address
// Address
//
$address = trim($_POST['address']);
if (empty($address)) {
$errors[] = 'Please enter an address.';
}
//
// City
$city = trim($_POST['city']);
if (empty($city)) {
$errors[] = 'Please enter a city.';
}
//
// State
$state = strtoupper(trim($_POST['state']));
if (empty($state)) {
$errors[] = 'Please enter a state.';
}
//
// Zip Code
$zip = trim($_POST['zip']);
if (empty($zip)) {
$errors[] = 'Please enter a zip code.';
}
else {
// See if we have a zip code matching 5 digits or
// 5 digits hyphen 4 digits
// NOTE: This was not a requirement.
if (!preg_match("#^[0-9]{5}(-[0-9]{4})?$#",$zip)) {
$errors[] = 'Please enter a valid zip code.';
}
}
if (count($errors) > 0){
// We have errors! Display error messages!
$body = '<div>';
$body .= '<h3>Errors!!</h3>';
foreach ($errors as $v) {
$body .= $v . '<br />';
}
$body .= '</div>';
}
else {
// No errors, calculate the coffe cost
if ( $coffeeType == 'Decaffeinated'){
//add one dollar to the price of coffee
$price_per_pound = $coffeeCosts[$coffeeCode] + 1.00;
}
else{
$price_per_pound = $coffeeCosts[$coffeeCode];
}
//calculate the total price
$total_price = $quantity * $price_per_pound;
// build the body of the html that includes the table with the results
$body = "<div><h3>Order Summary</h3>
<table>
<tr><td>Name:</td><td>$name</td></tr>
<tr><td>Address:</td><td>$address</td></tr>
<tr><td>City, State, Zip:</td><td>$city, $state, $zip</td></tr>
<tr><td>Phone #:</td><td>$phone</td></tr>
<tr><td>E-mail:</td><td>$email</td></tr></table>
<br />
</table><table border="1"><caption>Order Information</caption>
<tr><th>Coffee</th><th>Type</th><th>Quantity</th><th>Unit<br/>Cos
t</th>
<th>Total</th></tr>
<tr><td>$coffeeNames[$coffeeCode]</td>
<td>$coffeeType</td>
<td>$quantity lb(s)</td>
<td>$$price_per_pound</td>
<td>$" .
number_format($total_price, 2) .
'</td></tr>
</table></div>';
}
// Return to order form link
$body .= '<br />
<a href="coffee_form.html">Return to order form</a>';
// Display the html
?>
html file
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Coffee Order</title>
</head>
<body>
<h1>The Coffee House</h1>
<h3>Order Form</h3>
<form action="?PHP_SELF?" method="post">
<table>
<tr>
<td>Coffee:</td>
<td><select name="coffeeCode" id="Coffee">
<option value="">Select Coffee:</option>
<option value="bv">Boca Villa ($7.99/lb)</option>
<option value="sbr">South Beach Rhythm ($8.99/lb)</option>
<option value="pp">Pumpkin Paradise ($8.99/lb)</option>
<option value="ss">Sumatran Sunset ($9.99/lb)</option>
<option value="bb">Bali Batur ($10.95/lb)</option>
<option value="dd">Double Dark ($9.95/lb)</option>
</select>
</td>
</tr>
<tr>
<td>Type:</td>
<td>
<input type="radio" name="coffeeType" value="caf">Regular<br/>
<input type="radio" name="coffeeType" value="decaf">Decaffeinated
</td>
</tr>
<tr>
<td>Quantity (in pounds):</td>
<td>
<input type="text" name="quantity" maxlength="3" size="3"
id="Quantity">
</td>
</tr>
<tr>
<td>Name:</td>
<td>
<input type="text" name="name" id="Name">
</td>
</tr>
<tr>
<td>E-mail address:</td>
<td>
<input type="text" name="email" id="Email">
</td>
</tr>
<tr>
<td>Telephone #:</td>
<td>
<input type="text" name="phone" maxlength="14" size="14"
id="Telephone">
</td>
</tr>
<tr>
<td>Address:</td>
<td>
<input type="text" name="address" id="Address">
</td>
</tr>
<tr>
<td>City:</td>
<td>
<input type="text" name="city" id="City">
</td>
</tr>
<tr>
<td>State:</td>
<td>
<input type="text" name="state" maxlength="2" size="2"
id="State">
</td>
</tr>
<tr>
<td>Zip:</td>
<td>
<input type="text" name="zip" maxlength="10" size="10" id="Zip">
</td>
</tr>
<tr>
<td><input type="submit" value="Submit"></td><td><input
type="reset"></td>
</tr>
</table>
</form>
<?php echo $body ?>
</body>
</html>
Explanation / Answer
<?php
$coffeeNames['bv'] = 'Boca Villa';
$coffeeNames['sbr'] = 'South Beach Rhythm';
$coffeeNames['pp'] = 'Pumpkin Paradise';
$coffeeNames['ss'] = 'Sumatran Sunset';
$coffeeNames['bb'] = 'Bali Batur';
$coffeeNames['dd'] = 'Double Dark';
//Coffee prices array
$coffeeCosts['bv'] = 7.99;
$coffeeCosts['sbr'] = 8.99;
$coffeeCosts['pp'] = 8.99;
$coffeeCosts['ss'] = 9.99;
$coffeeCosts['bb'] = 10.95;
$coffeeCosts['dd'] = 9.95;
// Information has been posted, check and validate form items
//
// Check which coffee was picked
$coffeeCode = $_POST['coffeeCode'];
if (!isset($coffeeNames[$coffeeCode])) {
$errors[] = 'Please select a coffee to be purchased.';
}
//
// Check for regular/decaf
if (isset($_POST['coffeeType'])) {
$coffeeType = $_POST['coffeeType'];
}
else {
$coffeeType = NULL;
}
if ($coffeeType == "caf") {
$coffeeType = 'Regular';
}
elseif ($coffeeType == "decaf") {
$coffeeType = 'Decaffeinated';
}
else {
$errors[] = 'Please select regular or decaffeinated.';
}
//
// Check coffee quantity
// NOTE: There is more error checking that is required here
$quantity = $_POST['quantity'];
if (!is_numeric($quantity)) {
$errors[] = 'Please enter a numeric value for quantity.';
}
elseif ($quantity < 0.5) {
$errors[] = 'Please enter 0.5 pounds or greater for quantity.';
}
elseif (($quantity * 10) - intval($quantity * 10)) {
$errors[] = 'Please enter quantity in 0.5 pound increments.';
}
elseif ($quantity > 10) {
$errors[] = 'Please enter a quantity of 10 pounds or less.';
}
//
// Check order name
$name = trim($_POST['name']);
if (empty($name)) {
$errors[] = 'Please enter a name.';
}
//
// Check order email
// NOTE: There is more error checking than required for the email address
$email = trim($_POST['email']);
if (empty($email)) {
$errors[] = 'Please enter an e-mail address.';
}
else {
// Check to see if entered e-mail is valid
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = 'Please enter a valid e-mail address.';
}
}
//
// Check order phone
$phone = trim($_POST['phone']);
if (empty($phone)) {
$errors[] = 'Please enter a phone number.';
}
//
// Check various parts of order address
// Address
//
$address = trim($_POST['address']);
if (empty($address)) {
$errors[] = 'Please enter an address.';
}
//
// City
$city = trim($_POST['city']);
if (empty($city)) {
$errors[] = 'Please enter a city.';
}
//
// State
$state = strtoupper(trim($_POST['state']));
if (empty($state)) {
$errors[] = 'Please enter a state.';
}
//
// Zip Code
$zip = trim($_POST['zip']);
if (empty($zip)) {
$errors[] = 'Please enter a zip code.';
}
else {
// See if we have a zip code matching 5 digits or
// 5 digits hyphen 4 digits
// NOTE: This was not a requirement.
if (!preg_match("#^[0-9]{5}(-[0-9]{4})?$#",$zip)) {
$errors[] = 'Please enter a valid zip code.';
}
}
if (count($errors) > 0){
// We have errors! Display error messages!
$body = '<div>';
$body .= '<h3>Errors!!</h3>';
foreach ($errors as $v) {
$body .= $v . '<br />';
}
$body .= '</div>';
}
else {
// No errors, calculate the coffe cost
if ( $coffeeType == 'Decaffeinated'){
//add one dollar to the price of coffee
$price_per_pound = $coffeeCosts[$coffeeCode] + 1.00;
}
else{
$price_per_pound = $coffeeCosts[$coffeeCode];
}
//calculate the total price
$total_price = $quantity * $price_per_pound;
// build the body of the html that includes the table with the results
$body = "<div><h3>Order Summary</h3>
<table>
<tr><td>Name:</td><td>$name</td></tr>
<tr><td>Address:</td><td>$address</td></tr>
<tr><td>City, State, Zip:</td><td>$city, $state, $zip</td></tr>
<tr><td>Phone #:</td><td>$phone</td></tr>
<tr><td>E-mail:</td><td>$email</td></tr></table>
<br />
</table><table border="1"><caption>Order Information</caption>
<tr><th>Coffee</th><th>Type</th><th>Quantity</th><th>Unit<br/>Cos
t</th>
<th>Total</th></tr>
<tr><td>$coffeeNames[$coffeeCode]</td>
<td>$coffeeType</td>
<td>$quantity lb(s)</td>
<td>$$price_per_pound</td>
<td>$" .
number_format($total_price, 2) .
'</td></tr>
</table></div>';
}
// Return to order form link
$body .= '<br />
<a href="coffee_form.html">Return to order form</a>';
// Display the html
?>
html file
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Coffee Order</title>
</head>
<body>
<h1>The Coffee House</h1>
<h3>Order Form</h3>
<form action="?PHP_SELF?" method="post">
<table>
<tr>
<td>Coffee:</td>
<td><select name="coffeeCode" id="Coffee">
<option value="">Select Coffee:</option>
<option value="bv">Boca Villa ($7.99/lb)</option>
<option value="sbr">South Beach Rhythm ($8.99/lb)</option>
<option value="pp">Pumpkin Paradise ($8.99/lb)</option>
<option value="ss">Sumatran Sunset ($9.99/lb)</option>
<option value="bb">Bali Batur ($10.95/lb)</option>
<option value="dd">Double Dark ($9.95/lb)</option>
</select>
</td>
</tr>
<tr>
<td>Type:</td>
<td>
<input type="radio" name="coffeeType" value="caf">Regular<br/>
<input type="radio" name="coffeeType" value="decaf">Decaffeinated
</td>
</tr>
<tr>
<td>Quantity (in pounds):</td>
<td>
<input type="text" name="quantity" maxlength="3" size="3"
id="Quantity">
</td>
</tr>
<tr>
<td>Name:</td>
<td>
<input type="text" name="name" id="Name">
</td>
</tr>
<tr>
<td>E-mail address:</td>
<td>
<input type="text" name="email" id="Email">
</td>
</tr>
<tr>
<td>Telephone #:</td>
<td>
<input type="text" name="phone" maxlength="14" size="14"
id="Telephone">
</td>
</tr>
<tr>
<td>Address:</td>
<td>
<input type="text" name="address" id="Address">
</td>
</tr>
<tr>
<td>City:</td>
<td>
<input type="text" name="city" id="City">
</td>
</tr>
<tr>
<td>State:</td>
<td>
<input type="text" name="state" maxlength="2" size="2"
style="text-transform: uppercase" id="State">
</td>
</tr>
<tr>
<td>Zip:</td>
<td>
<input type="text" name="zip" maxlength="10" size="10" id="Zip">
</td>
</tr>
<tr>
<td><input type="submit" value="Submit"></td><td><input
type="reset"></td>
</tr>
</table>
</form>
<?php echo $body ?>
</body>
</html>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.