【发布时间】:2021-05-03 15:46:27
【问题描述】:
我正在创建一个食物菜单来练习 PHP 和 SQL 数据库。在我的数据库中,我有食物名称和这些食物的价格。在我的表格中,我有一个数字输入框,要求用户输入他们想要的食物数量。我想要做的是,如果我只订购 1 份披萨和一份薯条,我如何在用户点击提交按钮后显示总数
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
table,
tr,
th,
td {
border: solid 1px black;
}
</style>
</head>
<body>
<?php
echo '<form action="">';
echo"<h1>Food Menu</h1>";
$conn = mysqli_connect("localhost","root","","FoodMenu") or die("Missing Input. Go back and enter ID or Name");
$query = "select productname,price from product" ;
$r= mysqli_query($conn, $query);
echo"<p></p>";
echo"<table>";
echo"<tr><th>product</th><th>price</th><th>Quantity</th></tr>";
while($row = mysqli_fetch_assoc($r)){
printf("<tr><td>%s</td><td>%s</td><td>%s</td></tr>", $row["productname"],$row["price"], '<input type="number" id="quantity" name="quantity" value="0" min="0" max="5">');
}
echo"</table>";
echo '<p><input type="submit">';
echo '</form>';
?>
</body>
</html>
这是我的数据库信息
CREATE TABLE product (
productname varchar(30) NOT NULL,
price double NOT NULL, )
INSERT INTO product (productname, price) VALUES
('Pizza', 3.99),
('Fries', 1.99),
('water', 0.99),
('Cheese Balls', 2.99),
('Hot Dog', 2.99);
【问题讨论】:
-
表单应该有一个操作 href 链接,您将在其中进行所有请求的计算,然后打印一个带有结果值的新视图页面。还要向表单添加一个方法/类型属性,如 POST 或 GET。否则,您可以通过 jQuery 使用类似于 $.post / $.get / $.ajax 的东西并完全跳过提交。