我有以下代码不起作用。我的基于会话的购物车有问题。当我添加东西时,它没有显示,我不知道为什么。
主文件.php,前几行,他们收到将东西添加到购物车的信息:
//Start the session
session_start();
//Create 'cart' if it doesn't already exist
if (!isset($_SESSION['SHOPPING_CART'])){ $_SESSION['SHOPPING_CART'] = array(); }
//Add an item only if we have the required pices of information: name, qty
if (isset($_GET['name']) && isset($_GET['qty']) && isset($_GET['add'])){
//Adding an Item
//Store it in a Array
$ITEM = array(
//Item name
'name' => $_GET['name'],
//Item Price
//'price' => $_GET['price'],
//Qty wanted of item
'qty' => $_GET['qty']
);
//Add this item to the shopping cart
$_SESSION['SHOPPING_CART'][] = $ITEM;
//Clear the URL variables
header('Location: ' . $_SERVER['PHP_SELF']);
}
//Allowing the modification of individual items no longer keeps this a simple shopping cart.
//We only support emptying and removing
else if (isset($_GET['remove'])){
//Remove the item from the cart
unset($_SESSION['SHOPPING_CART'][$_GET['remove']]);
//Re-organize the cart
//array_unshift ($_SESSION['SHOPPING_CART'], array_shift ($_SESSION['SHOPPING_CART']));
//Clear the URL variables
header('Location: ' . $_SERVER['PHP_SELF'].'#cart');
}
else if (isset($_GET['empty'])){
//Clear Cart by destroying all the data in the session
session_destroy();
//Clear the URL variables
header('Location: ' . $_SERVER['PHP_SELF'].'#cart');
}
else if (isset($_POST['update'])) {
//Updates Qty for all items
foreach ($_POST['items_qty'] as $itemID => $qty) {
//If the Qty is "0" remove it from the cart
if ($qty == 0) {
//Remove it from the cart
unset($_SESSION['SHOPPING_CART'][$itemID]);
}
else if($qty >= 1) {
//Update to the new Qty
$_SESSION['SHOPPING_CART'][$itemID]['qty'] = $qty;
}
}
//Clear the POST variables
header('Location: ' . $_SERVER['PHP_SELF'].'#cart');
}
?>
这是应该显示购物车内容的div,但没有,它是空的,不知道为什么。它也在主楼内部.php
<div id="cart">
<form action="" method="post" name="shoppingcart" style="font-size:12px;">
<?php
//We want to include the shopping cart in the email
ob_start();
?>
<table width="500" border="1" id="searchTbl">
<tr>
<th scope="col">Edit</th>
<th scope="col">Description</th>
<th scope="col">Quantity</th>
</tr>
<?php
//Print all the items in the shopping cart
foreach ($_SESSION['SHOPPING_CART'] as $itemNumber => $item) {
?>
<tr id="item<?php echo $itemNumber; ?>">
<td><a href="?remove=<?php echo $itemNumber; ?>">Löschen</a></td>
<td><?php echo $item['name']; ?></td>
<!-- <td><?php echo $item['price']; ?></td> -->
<td><input name="items_qty[<?php echo $itemNumber; ?>]" type="text" id="item<?php echo $itemNumber; ?>_qty" value="<?php echo $item['qty']; ?>" size="2" maxlength="3" /></td>
<!-- <td><?php echo $item['qty'] * $item['price']; ?></td> -->
</tr>
<?php
}
?>
</table>
<?php $_SESSION['SHOPPING_CART_HTML'] = ob_get_flush(); ?>
<p>
<label>
<input type="submit" name="update" id="update" value="Update Cart" />
</label>
</p>
</form>
<p><a href="?empty">Empty Cart</a></p>
</div>
用于将商品添加到购物车的链接如下所示,$description从 MySQL 变量正确通过,因此可以工作。
<a rel='facebox' href='editqty.php?name={$description}'><img src='img/mail.png' width='25' ></a>
editqty.php 是一个弹出窗口,您可以在其中输入所需的金额并单击发送,然后引用主.php文件,该文件应通过执行 $_GET 来获取值:
<?php
try {
$id = $_GET['name'];
}
catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
?>
<form method="post" action="index.php">
<fieldset>
<legend>Amount?</legend>
<table>
<tr>
<td>Amount:</td>
<td><input type="text" name="qty" required value="" autocomplete="off"></td>
<td><input type="hidden" name="name" value="<?php echo $id; ?>" autocomplete="off"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="add" value="Add"></td>
</tr>
</table>
</fieldset>
</form>
现在我知道名称的值从 main.php,per GET 到 editqty.php,在那里它被用在一个隐藏值中,并且添加 qty 值,然后将这两个值发送回 main.php,这有效,我已经使用篡改数据对其进行了测试,所以我可以看到两个变量充满了通过 main.php 的信息。现在main.php的第一个脚本部分实际上似乎做了一些事情,它引用了正确的 #cart 并重新加载了页面,但是在该表中没有显示的内容...
我做错了什么?
与往常一样,提前感谢,每一个提示都非常感谢!
编辑:我刚刚发现,如果我将 URL 更改为:
<a href='main.php?name={$description}&qty=5&add=foobar'><img src='img/mail.png' width='25' ></a>
它就像一个魅力,所以我想它有一些事情要做,我正在调用另一个为我添加数量的 php 脚本,因为我在 main 中没有数量的输入.php我想用外部 php 来做到这一点......我必须保持会话活动状态或其他什么吗?
使用 POST 为表单发送我的变量而犯的一个基本错误,但主要的.php正在等待 GET。所以我只是将表单方法更改为 GET 并且它起作用了。