Ajax 不更新购物车 - Codeigniter


Ajax not updating the cart - Codeigniter

我正在使用ajax来更新codeigniter cart。我的问题是当我在购物车中添加多个商品并更新商品数量时,数量不会改变。它仅适用于最后添加的项目。

下面是我的代码

<form id="columnarForm" name="columnarForm" enctype="multipart/form-data" method="post">
<table width="100%" align="center" cellpadding="0" cellspacing="0" class="table-responsive table-bordered table-hover">
<tbody>
<?php foreach($products as $product){ ?>  
<tr>
<td align="right" valign="middle">
<input readonly type="text" name="name" value="<?php echo $product['name'] ?>"/>
</td>
<td align="right" valign="middle">
<input readonly type="text" name="price" value="<?php echo $product['price'] ?>"/>
</td>
<td align="right" valign="middle">
<input type="text" name="qty" value="<?php echo $product['qty'] ?>"/>
<input type="button" class="btn btn-primary btn-xs change" value="Change" />
</td>
<input type="hidden" name="rowid" value="<?php echo $product['rowid'] ?>"/>         
</tr>
<script type="text/javascript">
$(function(){
    $('.change').click(function(){
        $.ajax({
            type: "POST",
            url: "<?php echo base_url()?>update-shopping-cart",
            data: $("#columnarForm").serialize(),
            beforeSend: function(){
                $('#result').html('<img src="<?php echo site_url('assets/images/loading.gif'); ?>" class="center-block" />');
            },
            success: function(data){
                $('#result').html(data);
            }
        });
    });
});
</script>            
<? } ?>  
</tbody>
</table>
 </form>

我的控制器

// Updated the shopping cart
        function updateCart(){
        $data = array(
               'rowid'   => $this->input->post('rowid'),
               'qty'     => $this->input->post('qty'),
        );
        // Update the cart with the new information
        $this->cart->update($data);
        redirect(base_url().'update-cart-view');
        }

您正在遍历您的产品并将所有输入名称设置为相同内容:

<td align="right" valign="middle">
<input readonly type="text" name="name" value="<?php echo $product['name'] ?>"/>
</td>
<td align="right" valign="middle">
<input readonly type="text" name="price" value="<?php echo $product['price'] ?>"/>
</td>
<td align="right" valign="middle">
<input type="text" name="qty" value="<?php echo $product['qty'] ?>"/>

因此,您最终会得到x数量的"名称",x数量的"价格"和x数量的"数量"。你的代码用"价格"和"数量"获取一个元素"name"。提交时,由于所有输入都采用相同的表单,因此您发送所有信息,但仅处理一组信息。

'rowid'与每个元素名称一起使用 - 'price_rowid''name_rowid''qty_rowid',以便您可以遍历所有值,或者在提交时遍历所有发布数据。