【发布时间】:2021-05-25 07:09:09
【问题描述】:
所以我在 codeigniter 中编写了一个捐赠商店。我一直在努力为其添加购物车功能,并制作了一张小卡片,用于存放商店的物品,能够添加到购物车,以及发布和重新加载的 ajax 请求。问题是,在第一类中,它可以正常工作、成功并成功添加到购物车和重新加载。但是如果我进入其他类别并单击一个项目,它总是返回失败。
<div class="card">
<div class="card-header p-2">
<ul class="nav nav-pills">
<?php foreach ($category as $cat) { ?>
<li class="nav-item">
<a class="nav-link" href="#<?= $cat['categ_name']; ?>" data-toggle="tab"><?= $cat['categ_name']; ?></a></li>
<? } ?>
</ul>
</div><!-- /.card-header -->
<div class="card-body">
<div class="tab-content">
<?php foreach ($category as $pane) { ?>
<div class="tab-pane" id="<?= $pane['categ_name']; ?>">
<div class="row">
<?php foreach ($item as $ditems) { ?>
<?php if ($ditems['categ_name'] == $pane['categ_name']) { ?>
<div class="col-lg-3 col-6">
<div class="flip-card">
<div class="flip-card-inner">
<div class="flip-card-front">
<img src="<?= base_url() ?>assets/itemdb/<?= $ditems['image']; ?>" alt="Image" style="width:200px;height:160px;">
<h3><?= $ditems['item_name']; ?></h3>
</div>
<div class="flip-card-back">
<p><b>Price : $<?= $ditems['price']; ?></b></p>
<p><?php if ($ditems['package'] == 1) { ?>
<a href="">Package Details</a>
<?php } else { ?>
<p>Quantity : x <?= $ditems['amount']; ?></p>
<?= $ditems['descr']; ?></p>
<?php } ?>
<?php echo form_open(base_url('donation/add_cart_item'), 'class="horizontal-form" '); ?>
<div class="form-group">
<label for="quantity">Quantity:</label>
<input type="number" value="" class="form-control" id="quantity" name="quantity" placeholder="Enter Quantity">
</div>
<input type="number" value="<?= $ditems['catelog_id']; ?>" class="form-control" id="cat_id" name="cat_id" placeholder="<?= $ditems['catelog_id']; ?>" hidden>
<div class="form-group">
<button type="button" id="add_cart" class="btn" onClick="sendToCart();">Add To Cart</button>
</div>
<?php echo form_close(); ?>
</div>
</div>
</div>
</div>
<? } ?>
<? } ?>
</div>
</div>
<? } ?>
</div>
<!-- /.tab-content -->
</div><!-- /.card-body -->
</div>
<!-- /.nav-tabs-custom -->
这是我发送给它的脚本:
<?php
$url = base_url().'donation/add_cart_item';
?>
<script>
function sendToCart(){
var catId = $("#cat_id").val();
var qty = $("#quantity").val();
$.ajax({
type: "POST",
url: "<?php echo $url ?>",
data: { '<?php echo $this->security->get_csrf_token_name(); ?>':'<?php echo $this->security->get_csrf_hash(); ?>', cat_id: catId, quantity: qty, ajax: '1' },
dataType: "json",
cache:false,
success: function() {
location.reload();
alert("success");
},
error:function() {
alert("failure");
}
});
return false; // Stop the browser of loading the page
}
</script>
和控制器:
public function add_cart_item(){
$id = $this->input->post('cat_id');
$qty = $this->input->post('quantity');
$item = $this->cart_model->get_item($id);
$data = array(
'id' => $id,
'qty' => $qty,
'price' => $item['price'],
'name' => $item['item_name']
);
$data = $this->security->xss_clean($data);
$insert = $this->cart->insert($data);
if ($insert) {
if ($this->input->post('ajax') != '1') {
redirect('store'); // If javascript is not enabled, reload the page with new data
} else {
echo 'true'; // If javascript is enabled, return true, so the cart gets updated
}
} else {
$this->session->set_flashdata('warning', 'Could not insert the item to cart. Please try again later or contact an Administrator with this error.');
return false;
}
【问题讨论】:
-
那么错误信息是什么?
-
这:“它总是返回失败。” - 什么?并重复 epascarelgo - 错误信息是什么?
-
没有产生错误信息,唯一返回错误的是在 ajax 内部:alert("failure");我什至使用开发者工具来追踪它,但找不到问题
-
我有 3 个测试项目,一个在 Main 类别,一个在 Points 类别,一个在 Rares。第一个类别 Main 中的项目返回完全成功,只有当我进入其他类别时才会出现错误,它甚至不是控制器内部的插入错误,与 ajax 有关。
-
在搞砸了一点之后,我把它变成了一个警报来查看每个阶段,它不接受其他项目中的数量。所以第一个类别,那里的任何项目都接受传递的数量,但在其他类别中,它不传递数量。控制台内也有这个警告:[DOM] Found 2 elements with non-unique id #quantity,我猜它的数量 ID 不是唯一的,但是我如何将唯一的 id 作为数量传递并接受它javascript 的帖子?
标签: javascript php ajax codeigniter cart