我正在尝试从会话购物车中删除产品。我发送请求从Ajax到点击事件没有任何反应。这是我的代码。我没有得到任何回复,像按钮根本没有什么工作。我按下按钮以从会话数据中删除id并删除产品div。
function removeFromBasket(item) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
alert('item removed')
}
else if(xmlhttp.status == 400) {
alert("request error 400");
}
else{
alert("something went wrong");
}
};
xmlhttp.open("GET","cart.php?remove="+item,true);
xmlhttp.send();
}
cart.php
<?php
require "cfg.php";
if(isset($_GET['remove'])){
$id = $_GET['remove'];
$items = $_SESSION["cart"];
if(($key = array_search($id, $items)) !== false) {
unset($items[$key]);
$ses = $_SESSION["cart"] = array_values($items);
}
print_r($ses);
die();
}
?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
<?php
$cart = Session::get("cart");
$cartItems = array_keys($cart);
$q = "select * from items where id in (".implode(",",$cartItems).")";
$db = Database::getInstance();
$res = $db->_db->query($q);
foreach($res as $item){
?>
<form method="post" action="" class='.remove'>
<div style='border:1px solid blue;padding:4px;'>
<?=$item['name']?> (<?=$cart[$item['id']]?>)
<input class='glyphicon glyphicon-remove' onclick='removeFromBasket(<?= $cart[$item['id']] ?>)' name='submit' type='button' value='Remove' >
</div>
<?php
}
?>