Php 添加到购物车而不离开页面
我正在尝试进行ajax调用,以便在用户不必离开页面的情况下将项目添加到购物车。我已经设法写了下面的代码,但它是没有任何工作。在addtocart.php上,我手动输入了Php 添加到购物车而不离开页面,php,sql,ajax,pdo,Php,Sql,Ajax,Pdo,我正在尝试进行ajax调用,以便在用户不必离开页面的情况下将项目添加到购物车。我已经设法写了下面的代码,但它是没有任何工作。在addtocart.php上,我手动输入了ProdID、size和Category,但没有回显。有人能看看我的ajax和addtocart.php吗 AJAX <script> $(document).ready(function(){ $('.ajax').click(function(){ $.ajax({ url: '../main/php/a
ProdID
、size
和Category
,但没有回显。有人能看看我的ajax和addtocart.php吗
AJAX
<script>
$(document).ready(function(){
$('.ajax').click(function(){
$.ajax({
url: '../main/php/addtocart.php',
type: 'post',
data:{
length:$('#length').val(),
Category:$('#Category').val(),
id:$('#id').val(),
Qty:$('#Qty').val()
},
success: function(data) {
}
});
});
});
</script>
$(document).ready(function()
{
$('#button').click(function()
{
var id = $('#id').val();
var length = $('#length').val();
var qty = $('#Qty').val();
var cat = $('#Category').val();
$.ajax({
url: '../main/php/addtocart.php',
type: 'POST',
data: { id:id, length:length, qty:qty, cat:cat },
success: function(data)
{
//whatever you want to do
}
})
});
});
$(文档).ready(函数(){
$('.ajax')。单击(函数(){
$.ajax({
url:“../main/php/addtocart.php”,
键入:“post”,
数据:{
长度:$('#length').val(),
类别:$(“#类别”).val(),
id:$('#id').val(),
数量:$('数量').val()
},
成功:功能(数据){
}
});
});
});
PHP
<?php
include('dbconnect.php');
$id = $_POST['id'];
$length = $_POST["size"];
$qty = $_POST['Qty'];
$Category = $_POST['Category'];
$stmt = $conn->prepare("
SELECT ProductName, Category.Name, size, Price
FROM itembag, Product, Category
WHERE Product.ProdID =:id
AND size= :length AND Category.Name = :Category Limit 1");
$stmt->bindParam('id',$id);
$stmt->bindParam('length',$length);
$stmt->bindParam('Category',$Category);
$stmt->execute();
$i=0;
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
if ($i == 0) {
echo 'status:1,id:'.$row['ProdID'].',Price:'.$row['Price'].',txt:\'\
\
<table width="100%" id="table_'.$row['ProdID'].'">\
<tr>\
<td width="60%">'.$row['ProductName'].'</td>\
<td width="40%">$'.$row['Price'].'</td>\
<td width="10%">$'.$row['Category'].'</td>\
</tr>\
</table>\'';
}
}
?>
附加信息:一个项目可能有一个id,但它有不同的大小和许多类别 也许使用jQuery load可以帮助您加载/更新购物车。
缺少要发布的数据
data: {
//Here you have to put your data
},
您必须获得要发布的值
例如:
('.ajax').click(function(){
pid = $('#pid').val();//#pid->is the id of your input, the same bellow
length = $('#length').val();
Qty = $('#Qty').val();
Category = $('#Category').val();
$.ajax({
url: '../main/php/addtocart.php',
data:{
pdi:pdi,
length:length,
Qty :Qty ,
Category : Category},
type: 'post',
}).done(function(data) {
$(data).html('#containerShoppingCart');//Callback Replace the html of your shoppingCart Containe with the response of addtocart.php
}).fail(function(){ alert("failed!"); });//Some action to indicate is Failing
您可以根据
HTML
查看以获取更多信息,您的AJAX
如下所示:
注意:如果您使用的是AJAX
,则必须通过id
而不是name
引用选择器
AJAX
<script>
$(document).ready(function(){
$('.ajax').click(function(){
$.ajax({
url: '../main/php/addtocart.php',
type: 'post',
data:{
length:$('#length').val(),
Category:$('#Category').val(),
id:$('#id').val(),
Qty:$('#Qty').val()
},
success: function(data) {
}
});
});
});
</script>
$(document).ready(function()
{
$('#button').click(function()
{
var id = $('#id').val();
var length = $('#length').val();
var qty = $('#Qty').val();
var cat = $('#Category').val();
$.ajax({
url: '../main/php/addtocart.php',
type: 'POST',
data: { id:id, length:length, qty:qty, cat:cat },
success: function(data)
{
//whatever you want to do
}
})
});
});
如果您在PHP
我正在尝试向cartyour
数据
对象添加一个项目。在ajax调用中,数据:{}
@ChristianGärtner现在花了很长时间,addtocart.php呢?好的,您的addtocart.php
需要4个参数,您只从AJAX传递了2个参数,你可以发布你的HTML
代码吗?@aw我刚刚添加了类别。Name
在PHP中不起作用。你已经发送了一个对象的json编码版本,并在服务器端对其进行json解码。我有一个添加到购物车的按钮,它不在表单中,因为你正在使用AJAX
我看到你发布了var
yes,因为基于您的PHP
code,这些是您希望从AJAX
request@Þaw获得的数据,我刚刚使用了窗口。警报(数据)
要查看数据是否已传递并返回,请将值传递到addtocart.php,如何将值显示为购物车的一种形式@埃米利奥·戈尔