PHP中Add to Cart按钮无法添加商品至购物车问题求助
购物车添加按钮无响应问题修复
我帮你排查了代码里的问题,点击「Add to Cart」按钮没反应主要是变量名拼写错误和潜在的文件名大小写不一致导致的,下面逐一拆解并给出修复方案:
1. 核心问题:变量名拼写错误
在判断商品是否已存在于购物车的逻辑中,你错误地给变量加了多余的下划线:$_item_array_id,正确的变量名应该是$item_array_id。这个错误会直接导致商品存在性判断逻辑失效,无法将商品添加到session购物车中。
错误代码片段:
if(isset($_SESSION["shopping-cart"])) { $_item_array_id = array_column($_SESSION["shopping-cart"], "item_id"); // 此处多了一个下划线 if(!in_array($_GET["id"], $item_array_id)) { // 商品添加逻辑 } }
修复后代码:
if(isset($_SESSION["shopping-cart"])) { $item_array_id = array_column($_SESSION["shopping-cart"], "item_id"); // 移除多余下划线 if(!in_array($_GET["id"], $item_array_id)) { $item_array = array( 'item_id' => $_GET["id"], 'item_product' => $_POST["hidden_product"], 'item_price' => $_POST["hidden_price"], 'item_quantity' => $_POST["quantity"] ); array_push($_SESSION['shopping-cart'], $item_array); } else { echo '<script>alert("Item has already been added to your shopping basket")</script>'; echo '<script>window.location="shoppingcart.php"</script>'; } }
2. 潜在问题:文件名大小写不一致
你的表单action属性里写的是shoppingcart.php,但PHP代码中的跳转语句用的是ShoppingCart.php。如果你的服务器是Linux系统(区分文件名大小写),这会导致页面跳转异常,甚至无法触发PHP逻辑。建议统一文件名大小写,比如全部使用小写:
修改表单的action属性:
<form method="post" action="shoppingcart.php?action=add&id=<?php echo $row["id"];?>">
同步修改PHP中的跳转代码:
echo '<script>window.location="shoppingcart.php"</script>';
3. 额外优化建议
- 修复删除按钮显示:当前删除按钮是空的
<span class=""></span>,可以添加可见的标识,比如:<td><a href="shoppingcart.php?action=delete&id=<?php echo $values["item_id"]; ?>" class="text-danger">×</a></td> - 添加数量合法性校验:可以用JS或PHP校验用户输入的数量为正整数,避免非法值存入购物车,比如在表单提交前添加JS校验:
document.querySelector('form').addEventListener('submit', function(e) { const quantity = this.querySelector('[name="quantity"]').value; if(!/^\d+$/.test(quantity) || parseInt(quantity) < 1) { e.preventDefault(); alert('Please enter a valid positive quantity'); } });
完整修复后的核心PHP代码
<?php session_start(); $connect = mysqli_connect("localhost", "root", "", "stock"); if(isset($_POST["add_to_cart"])) { if(isset($_SESSION["shopping-cart"])) { $item_array_id = array_column($_SESSION["shopping-cart"], "item_id"); if(!in_array($_GET["id"], $item_array_id)) { $item_array = array( 'item_id' => $_GET["id"], 'item_product' => $_POST["hidden_product"], 'item_price' => $_POST["hidden_price"], 'item_quantity' => $_POST["quantity"] ); array_push($_SESSION['shopping-cart'], $item_array); } else { echo '<script>alert("Item has already been added to your shopping basket")</script>'; echo '<script>window.location="shoppingcart.php"</script>'; } } else { $item_array = array( 'item_id' => $_GET["id"], 'item_product' => $_POST["hidden_product"], 'item_price' => $_POST["hidden_price"], 'item_quantity' => $_POST["quantity"] ); $_SESSION["shopping-cart"][0] = $item_array; } } if(isset($_GET["action"])) { if($_GET["action"] == "delete") { foreach($_SESSION["shopping-cart"] as $keys => $values) { if($values["item_id"] == $_GET["id"]) { unset($_SESSION["shopping-cart"][$keys]); echo '<script>alert("Item removed from cart")</script>'; echo '<script>window.location="shoppingcart.php"</script>'; } } } } ?>
内容的提问来源于stack exchange,提问作者user3018640
相关产品推荐
相关产品推荐

