The purpose of this code is to log the inputs of a form (which is a menu for a shop) to the database to be kept as records as well as printed out as a receipt at the store.
I have been learning how to code from the internet and tried to put together my knowledge in a project. The code is working, but I would like to adopt elegance and good coding practices.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<?php
require 'databaseaccess.php';
?>
</head>
<body>
<?php
//check order present
if(!($_POST))
{
echo "error, order was not received!";
echo "<br>";
echo "return to <a href='orderformwscart.html'> order page </a>";
}
else{
$total = 0;
$soba_size = $_POST['soba_size'];
switch($soba_size){
case "small":
$total = $total + 4;
break;
case "reg":
$total = $total + 6;
break;
case "large":
$total = $total + 8;
break;
}
$soba_type = $_POST['soba_type'];
$dbtoppings = '';
if ($_POST['topping']){
foreach($_POST['topping'] as $topping){
switch($topping){
case "duck":
$total = $total + 1;
$dbtoppings = $dbtoppings.",".$topping;
break;
case "egg":
$total = $total + 2;
$dbtoppings = $dbtoppings.",".$topping;
break;
case "chicken":
$total = $total + 3;
$dbtoppings = $dbtoppings.",".$topping;
break;
case "teriyaki chicken":
$total = $total + 4;
$dbtoppings = $dbtoppings.",".$topping;
break;
}
}
}
if($_POST['requests']){
$request = $_POST['requests'];
}
else{
$request = '';
}
//begin data entry
$data1 = array('total' => $total);
$STH = $DBH->prepare("INSERT INTO orderlist(total) values (:total)");
$STH->execute($data1);
$lastId = $DBH->lastInsertId();
//dataentry2
$data2 = array('orderid' => $lastId, 'soba_size' => $soba_size, 'soba_type' => $soba_type, 'topping' => $dbtoppings,'request' => $request);
$STH = $DBH->prepare("INSERT INTO orderdetails(orderID,soba_size,soba_type,topping,request) values (:orderid,:soba_size,:soba_type,:topping,:request)");
$STH->execute($data2);
}
?>
<div class="jumbotron">
<h2 class="text-center">Thank you for ordering. Your order has been printed.
<p id ="time">
</p> </h2>
<h3 class="text-center">Order Number: <?php echo $lastId; ?>
</br>
Amount:$ <?php echo $total; ?> </h3>
<h4 class="text-center"><small>Please show this page at the cashier to confirm your order. Thank you!</small></h4>
</div>
</body>
</html>
else
, you can just addreturn;
as the last line ofif(!($_POST))
's body. Or even better: Move that if to the very top of the file and change it's body toheader('Location: orderformwscart.html');
exit;
\$\endgroup\$