Can someone please tell me why this sticky form doesn\'t work? Thanks! <?php $co
ID: 3605716 • Letter: C
Question
Can someone please tell me why this sticky form doesn't work? Thanks!
<?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;
// 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
$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
$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.';
}
if (count($errors) > 0){
// Display errors
$body = '<div>';
$body .= '<h3>Errors!!</h3>';
foreach ($errors as $v) {
$body .= $v . '<br />';
}
$body .= '</div>';
}
else {
// Calculate Coffee Cost, adding one dollar for decaffeinated
if ( $coffeeType == 'Decaffeinated'){
$price_per_pound = $coffeeCosts[$coffeeCode] + 1.00;
}
else {
$price_per_pound = $coffeeCosts[$coffeeCode];
}
// Calculate Total Price
$total_price = $quantity * $price_per_pound;
// Display order
$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/>Cost</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
$body .= '<br />
<a href="coffee_form.html">Return to order form</a>';
// Display the html
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
<html xmlns= "http://www.w3.org/1999/xhtml" >
<head>
<meta http-equiv= "Content-Type" content= "text/html; charset=utf-8"
/>
<title> Coffee Order </title>
</head>
<body>
<?php echo $body ?>
</body>
</html>
Explanation / Answer
Now it is working well. In the code that you have provided some syntax errors are there. I have modified them.
<?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;
// 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
$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
$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.';
}
if (count($errors) > 0){
// Display errors
$body = '<div>';
$body .= '<h3>Errors!!</h3>';
foreach ($errors as $v) {
$body .= $v . '<br />';
}
$body .= '</div>';
}
else {
// Calculate Coffee Cost, adding one dollar for decaffeinated
if ( $coffeeType == 'Decaffeinated'){
$price_per_pound = $coffeeCosts[$coffeeCode] + 1.00;
}
else {
$price_per_pound = $coffeeCosts[$coffeeCode];
}
// Calculate Total Price
$total_price = $quantity * $price_per_pound;
// Display order
$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/>Cost</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
$body .= "<br/>";
$body .= '<a href="coffee_form.html">Return to order form</a>';
// Display the html
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >
<html xmlns= "http://www.w3.org/1999/xhtml" >
<head>
<meta http-equiv= "Content-Type" content= "text/html; charset=utf-8"
/>
<title> Coffee Order </title>
</head>
<body>
<?php echo $body ?>
</body>
</html>