As a long time Java user, the idea of conditional statements is not new to me. I've recently begun learning php and came across a difficulty when trying to access data from a dropdown list and displaying it. Here is the HTML:
<!DOCTYPE html>
<html>
<head>
<title>Bob's Auto Parts - Place an Order</title>
</head>
<body>
<form action = "/processorder.php" method="post">
<table style = "border: opx;">
<tr style = "background: #cccccc;">
<td style = "width: 150px; text-align: center;">Item</td>
<td style = "width: 15px; text-align: center;">Quantity</td>
</tr>
<tr>
<td>Tires</td>
<td><input type = "text" name = "tireqty" size = "3" maxlength = "3" /></td>
</tr>
<tr>
<td>Oil</td>
<td><input type = "text" name = "oilqty" size = "3" maxlength = "3" /></td>
</tr>
<tr>
<td>Spark Plugs</td>
<td><input type = "text" name = "sparkqty" size = "3" maxlength = "3" /></td>
</tr>
<tr>
<td>How did you find Bob's</td>
<td><select name = "find">
<option value = "a">I'm a regular customer</option>
<option value = "b">TV advertising</option>
<option value = "c">Phone directory</option>
<option value = "d">Word of mouth</option>
</select>
</td>
</tr>
<tr>
<td colspan = "2" style = "text-align: center;"><input type =
"submit" value = "Submit Order" /></td>
</tr>
</table>
</form>
</body>
</html>
And the corresponding php:
<!DOCTYPE html>
<html>
<head>
<title>Bob's Auto Parts - Order Results</title>
</head>
<body>
<h1>Bob's Auto Parts</h1>
<h2>Order Results</h2>
<?php
echo "<p>Order processed at: ";
echo date('H:i, jS F Y');
echo "</p>";
// create short variable names
$tireqty = $_POST['tireqty'];
$oilqty = $_POST['oilqty'];
$sparkqty = $_POST['sparkqty'];
echo '<p>Your order is as follows: </p>';
echo htmlspecialchars($tireqty). ' tires<br / >';
echo htmlspecialchars($oilqty). ' bottles of oil<br / >';
echo htmlspecialchars($sparkqty). ' spark plugs<br / >';
/* is the same as...
echo '<p>Your order is as follows: </p>';
echo htmlspecialchars($tireqty); echo ' tires<br / >';
echo htmlspecialchars($oilqty); echo ' bottles of oil<br / >';
echo htmlspecialchars($sparkqty); echo ' spark plugs<br / >';
*/
$totalqty = 0;
$totalqty = $tireqty + $oilqty + $sparkqty;
echo "<p>Items ordered: ".$totalqty."<br />";
$totalamount = 0;
define('TIREPRICE', 100);
define('OILPRICE', 10);
define('SPARKPRICE', 4);
$totalamount = $tireqty * TIREPRICE +
$oilqty * OILPRICE +
$sparkqty * SPARKPRICE;
echo "Subtotal: $".number_format($totalamount, 2)."<br />" ;
$taxrate = 0.10; // local sales tax is 10%
$totalamount = $totalamount * (1 + $taxrate);
echo "Total including tax: $".number_format($totalamount, 2)."</p>";
echo '$find';
/* good for making sure forms are filled out...
echo 'isset($tireqty): '.isset($tireqty).'<br />';
echo 'isset($nothere): '.isset($nothere).'<br />';
echo 'empty($tireqty): '.empty($tireqty).'<br />';
echo 'empty($nothere): '.empty($nothere).'<br />';
*/
if ($find == "a"){
echo "<p>Regular Customer</p>";
}
elseif ($find == "b"){
echo "<p>Customer referred by TV advert</p>";
}
elseif ($find == "c"){
echo "<p>Customer referred by phone directory</p>";
}
elseif ($find == "d"){
echo "<p>Customer referred by word of mouth</p>";
}
else {
echo "<p>We do not know how this customer found us</p>";
}
?>
</body>
</html>
Simply put, the results page always displays what is contained in the "else" statement, even when another option is pressed. Any advice/suggestions are really appreciated. Thanks.