我正在开发一个购物车系统,一切都很完美,除了一件事
好吧,我使用PHP SESSION存储每个产品的数据,包括ID,名称,价格和数量。除非价格变量,否则所有变量都已正确完成。我不知道为什么!我使用jQuery来获取值并将它们发送到将在SESSION中处理它们的页面。
我将把代码的相关部分放在一边以方便。这是我的 products.php :
<?php
session_start();
?>
<html>
<body>
<?php
$connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
$sql = "SELECT id, name, price FROM products ORDER BY id";
$result = $connection->query($sql);
if ($result->num_rows > 0)
{
echo "<table>";
echo " <tr>";
echo " <th>ID</th>";
echo " <th>NAME</th>";
echo " <th>PRICE</th>";
echo " <th>QUANTITY</th>";
echo " </tr>";
while($row = $result->fetch_assoc())
{
echo " <tr>";
echo " <td><div class='id-product'>". $row["id"]."</div></td>";
echo " <td><div class='name-product'>". $row["name"]."</div></td>";
echo " <td>$<div class='price-product'>". $row["price"]."</div></td>";
echo " <form class='add'>";
echo " <td><input type='number' name='quantity' value='1' min='1'/></td>";
echo " <td><button type='submit'>Add</button></td>";
echo " </form>";
echo " </tr>";
}
echo "</table>";
}
$connection->close();
?>
<script>
$(document).ready(function()
{
$('.add').on('submit', function()
{
var id = $(this).closest('tr').find('.id-product').text();
var name = $(this).closest('tr').find('.name-product').text();
var price = $(this).closest('tr').find('.price-product').val();
var quantity = $(this).closest('tr').find('input').val();
window.location.href = "add.php?id=" + id + "&name=" + name + "&price=" + price + "&quantity=" + quantity;
return false;
});
});
</script>
<body>
</html>
现在在 add.php 我有以下内容:
<?php
session_start();
$id = isset($_GET['id']) ? $_GET['id'] : "";
$name = isset($_GET['name']) ? $_GET['name'] : "";
$price = isset($_GET['price']) ? $_GET['price'] : "";
$quantity = isset($_GET['quantity']) ? $_GET['quantity'] : "";
$cart_item = array
(
'id' => $id,
'name' => $name,
'price' => $price,
'quantity' => $quantity
);
if(!isset($_SESSION['cart']))
{
$_SESSION['cart'] = array();
}
if(array_key_exists($id, $_SESSION['cart']))
{
header('Location: products.php?action=exists&id=' . $id . '&name=' . $name);
}
else if($quantity <= 0)
{
header('Location: products.php?action=invalidquantity&id=' . $id . '&name=' . $name);
}
else
{
$_SESSION['cart'][$id] = $cart_item;
header('Location: products.php?action=added&id=' . $id . '&name=' . $name);
}
?>
要显示SESSION中存在的产品,我在页面 cart.php 中使用 foreach :
$total = 0;
foreach($_SESSION['cart'] as $id)
{
echo "<tr>";
echo "<td>{$id['name']}</td>";
echo "<td>${$id['price']}</td>";
echo "<td>{$id['quantity']}</td>";
echo "<td>";
echo "<a href='remove.php?id={$id['id']}&name={$id['name']}'>Remove</a>";
echo "</td>";
echo "</tr>";
$total += ($id['price'] * $id['quantity']);
}
正如我之前所说,我已经返回了ID,名称和数量的值,但价格仍然是空的。怎么了?
答案 0 :(得分:3)
你不相信你的JS中的价格。
window.location.href = "add.php?id=" + id + "&name=" + name + "&price" + price + "&quantity=" + quantity;
return false;
应该是
window.location.href = "add.php?id=" + id + "&name=" + name + "&price=" + price + "&quantity=" + quantity;
return false;
答案 1 :(得分:2)
请参阅jquery&.39和.text
中的区别Difference between val() and text()
试试这个:
var price = $(this).closest('tr').find('.price-product').text();