gpt4 book ai didi

javascript - 将商品添加到购物车

转载 作者:行者123 更新时间:2023-11-30 23:03:01 25 4
gpt4 key购买 nike

我是 php 新手,所以请原谅我的知识不足。我正在开发一个简单的购物车。我将屏幕分为三个 framset。左侧是一个 imagemap,用户可以在其中单击所需的项目。 topRight 是一个 php 页面,它检索并在表格中显示在 leftFrame 中单击的项目的详细信息。该表的最后一个单元格有一个 textfield 和一个用于提交订单的 buttonbottomRight 框架的目标是显示购物车,向用户显示已经选择了多少产品。我的问题是到目前为止我只能显示当前提交的订单。换句话说,我只显示 topRigt 框架中“订购”的最后一项的详细信息。我怎样才能继续添加第二个项目,而不是替换它?

这是topLeft

的代码
<html>
<head>
<title>Top Right</title>
<style type="text/css">
<!--
@import url("style.css");
-->
</style>
</head>
<body bgcolor = "#E0E6F8">
<h2>ITEMS</h2>
<?php
session_start();

// get the id from the left frame
$id = $_GET['var'];

// variable to connect to db
$user_name = "name";
$password = "password";
$server = "server";
$link = new mysqli($server,$user_name,$password,'poti');

if(!$link)
die("Could not connect to databse");

$result = $link->query("SELECT * FROM products WHERE product_id = '$id';");

while($row = $result->fetch_array())
{
$id = $row['product_id'];
$name = $row['product_name'];
$unit_price = $row['unit_price'];
$unit_quantity = $row['unit_quantity'];
$in_stock = $row['in_stock'];
$arrayName = array('id' => $id, 'name' => $name);
// store values in session
$_SESSION['cart'] = $arrayName;

// display details in a table (code needs to be refactored)
if(!empty($row)){
print "";
print "<form method='post' name='add' id='add' action='' target=''>";
print "<table id='hor-minimalist-a' border = '0' width = '100%'>";
print "<thead>
<tr>
<th scope='col'>ID</th>
<th scope='col'>Name</th>
<th scope='col'>Price</th>
<th scope='col'>QTY</th>
<th scope='col'>In Stock</th>
<th scope='col'>Order</th>
</tr>
</thead>";
print "<tr>\n";
print "<td>";
print($id);
print "</td>";
print "<td>";
print($name);
print "</td>";
print "<td>";
print($unit_price);
print "</td>";
print "<td>";
print($unit_quantity);
print "</td>";
print "<td>";
print($in_stock);
print "</td>";

print "<td>
<input type='text' name='qty_ordered' size='4'><input type='submit' name='submit' value='Submit' onclick='checkQty()'>
</td>";
print "</tr>";

}
print"</table>";
print "</form>";
}
?>


<script type="text/javascript">

function checkQty()
{
if(document.add.qty_ordered.value > 0 && document.add.qty_ordered.value <= 20)
{
document.add.action = "bottomRight.php";
document.add.target = "BottomRight";
document.add.submit();
}
else
{
//document.add.action = "topRight.php";
//document.add.target = "TopRight";
alert("Quantity must be between 1 and 20, please enter a value between 1 to 20");
//document.add.submit();
}
}
</script>
</body>
</html>

对于bottomRight:

<html>
<head>
<title>Bottom Right</title>
<style type="text/css">
<!--
@import url("style.css");
-->
</style>
</head>
<body>
<h2>Shopping Cart</h2>
<?php
session_start();
// doing this I keep on displaying only the last item "ordered"
if (isset($_POST['submit'])===true) {
if(isset($_SESSION['cart'])===true) {
$array = array($_SESSION['cart']);
print_r($array);

// not working
/*foreach($array as $a)
{
echo "<table><tr>";
echo "<td width='150px'><span style='color:white'>" . $a['id'] ."</span></td>";
echo "</tr></table>";}*/
}
}
?>
</body>
</html>

最佳答案

在 youtube 上查看 Adam Khoury 提供的视频教程。他使用了 mysql_*(我知道它已被弃用)函数,但您可以使用自己的代码来解决问题。该视频仅供您引用。

这是 youtube 播放列表链接:https://www.youtube.com/playlist?list=PL442E340A42191003

这里是解决您查询的具体视频链接:https://www.youtube.com/watch?v=WXqbQy9fOp8

希望对你有帮助。

关于javascript - 将商品添加到购物车,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23147942/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com