Ajax 未更新购物车 - Codeigniter

Ajax not updating the cart - Codeigniter

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

下面是我的代码

<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 数量的 "name"s,x 数量的 "price"s 和 x 数量的 "qty"秒。您的代码使用 "price" 和 "qty" 获取元素 "name"。提交时,因为所有输入都采用相同的形式,所以您发送所有信息,但只处理一组。

'rowid' 与每个元素名称一起使用 - 'price_rowid''name_rowid''qty_rowid' 这样您就可以遍历所有值,或者遍历所有 post 数据提交。