在这种情况下,我要从数据库中获取产品ID,并将其放入data-idproduct中:
<a class="cart" href="" data-idproduct="<?php echo $itemArtikal['id_product'] ?>">Add to Cart</a>
单击该“添加到购物车”按钮,我将获取存储的data-idproduct并将其发送到cart-process.php页面。
这是我的main.js代码:
$(document).ready(function (){
$('.cart').click(function (e) {
e.preventDefault();
let dataIdArtikla = $(this).data('idproduct');
$.ajax({
method: "post",
url: "models/cart-process.php",
contentType: "application/json; charset=utf-8",
data: JSON.stringify({
"idProd" : dataIdArtikla
}),
success: function (response) {
alert(response);
},
error: function (xhr, textStatus) {
alert("oh no: "+ " " + xhr.status+ " " +textStatus.responseText);
}
})
});
})
这是基本的cart-process.php代码:
$idProduct = $_POST['idProd'];
header('Content-type: application/json');
echo json_encode([
'id_product' => $idProduct
]);
它始终返回错误,并且JSON的响应始终为null来自“网络”选项卡的响应:
<br />
<b>Warning</b>: Undefined array key "idProd" in <b>C:\xampp\htdocs\freshshop01\models\cart-process.php</b> on line <b>5</b><br />
{"id_artika":null}
通常,其背后的想法是,当用户单击“添加到购物车”按钮时,将产品添加到购物车中。我试图传递被点击产品的ID,然后在cart-process.php中显示具有相同ID的整个产品信息。
我不知道如何解决它,有人可以帮忙吗?
编辑:
这是我的新的cart-process.php文件:
<?php
$idArtikla = $_POST['idProd'];
if(isset($idArtikla)) {
require_once "../config/connection.php";
session_start();
$_SESSION['idArtikla'] = $idArtikla;
$queryCart = $conn -> prepare('SELECT * FROM artikal WHERE id_artikal = :idArtikal');
$queryCart->bindParam(":idArtikal", $idArtikla);
try {
$queryCart->execute();
$resultCart = $queryCart->fetchAll(PDO::FETCH_ASSOC);
} catch (PDOException $exception) {
echo json_encode(['error' => $exception->getMessage()]);
}
header('Content-type: application/json');
echo json_encode($resultCart);
}
和新的main.js文件:
$(document).ready(function (){
$('.cart').click(function (e) {
e.preventDefault();
let dataIdArtikla = $(this).data('idartikla');
$.ajax({
method: "post",
url: "models/cart-process.php",
dataType: 'json',
data: {
"idProd" : dataIdArtikla
},
success: function (response) {
console.log("Success message: " + response);
},
error: function (xhr, textStatus) {
alert("oh no: "+ " " + xhr.status+ " " +textStatus.responseText);
}
})
});
})
响应现在返回整个对象,但不会成功。输入错误。我怀疑对象到json转换有问题,反之亦然,但不确定在这里到底有什么问题?这是响应和请求的模样:
帖子有点长,我请您帮忙。
传递数据时,您必须向JSON添加密钥。代替contentType,使用dataType:'json'告诉jQuery期望什么。
$.ajax({
method: "post",
url: "models/cart-process.php",
dataType: 'json',
data: {'json':JSON.stringify({"idProd" : dataIdArtikla})},
success: function (response) {
alert(response);
},
error: function (xhr, textStatus) {
alert("oh no: "+ " " + xhr.status+ " " +textStatus.responseText);
}
})
在php中,您可以像这样访问元素:
echo (json_decode($_POST['json'])->idProd);
一种更简单的方法是只传递数据而不进行序列化
$.ajax({
method: "post",
url: "models/cart-process.php",
data: {"idProd" : dataIdArtikla},
success: function (response) {
alert(response);
},
error: function (xhr, textStatus) {
alert("oh no: "+ " " + xhr.status+ " " +textStatus.responseText);
}
})
然后,您可以直接通过$ _POST访问idProd:
echo ($_POST['idProd']);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句