【问题标题】:Codeigniter Cart: How to add multiple items with ajax and jqueryCodeigniter 购物车:如何使用 ajax 和 jquery 添加多个项目
【发布时间】:2013-02-21 06:48:36
【问题描述】:

我正在使用 Codeigniter 构建一个基于 ajax 的购物车,并且添加/删除功能可以完美运行。我现在正在尝试添加用于添加多个项目的选项,但无法使其正常工作。

这是我正在使用的标记。我不确定它是否是最好的设计,但它与非 ajax 功能一起使用,所以我想应该没问题。

<form action="cart/add_multiple" method="post" accept-charset="utf-8">
<input type="hidden" name="items[0][id]" value="3571310" />
<input type="hidden" name="items[0][qty]" value="1" />
<input type="hidden" name="items[0][price]" value="59.00" />
<input type="hidden" name="items[0][name]" value="London" />
<input type="hidden" name="items[0][heb_name]" value="לונדון" />
<input type="hidden" name="items[0][full_price]" value="59.00" />
<input type="hidden" name="items[0][discount_price]" value="59.00" />

<input type="hidden" name="items[1][id]" value="7397903" />
<input type="hidden" name="items[1][qty]" value="1" />
<input type="hidden" name="items[1][price]" value="29.00" />
<input type="hidden" name="items[1][name]" value="London Triple" />
<input type="hidden" name="items[1][heb_name]" value="לונדון טריפל" />
<input type="hidden" name="items[1][full_price]" value="29.00" />
<input type="hidden" name="items[1][discount_price]" value="29.00" />
<input type="submit" name="add_multi" value="add to cart"  /></form>

ajax脚本如下:

$(document).ready(function() {
$(document).on("submit", "div#winning_combo_small form", function () { //catches every click on the submit button of the "add to cart" form
    var items = $(this).serialize();

    alert(items);
    $.post(base_url + "cart/add_multiple", {items: items, ajax: '1' },
            function(data){
                if (data =='true')
                    { // Interact with returned data
                        $.get(base_url + "cart", function(cart){ // Get the contents of the url cart/show_cart
                        $("#cart_sidebar").html(cart);
                        })

                        $.get(base_url + "cart/count_items", function(items){
                            $("#cart_items").html(items);
                        })
                    }
                });
  return false;
})
});

但它不起作用,因为add_multiple 函数接收数据作为字符串,而不是数组。我是否必须以某种方式解码数据才能将其转换为数组?希伯来语字符是否会妨碍并把事情搞砸?

我应该说,当以常规方式发布表单时,没有 ajax,项目被添加到购物车并且一切正常。那么普通post和ajax post有什么区别呢?

【问题讨论】:

    标签: codeigniter jquery shopping-cart


    【解决方案1】:

    好吧,我让它工作了,虽然我不确定这是否是最优雅的方式。

    这就是我所做的:

    在 ajax 脚本中,我将 var items = $(this).serialize(); 更改为 var items = $(this).serializeArray();。我现在得到一个数组而不是字符串,但这不是我需要插入购物车的格式。所以我循环这个数组以创建一个所需格式的数组,并使用这个新数组插入到购物车中。

    这是我在购物车控制器下的 add_multiple 函数:

     function add_multiple()
    {
        $items = $this->input->post('items');
        $ajax = $this->input->post('ajax');
    
        // Check if user has javascript enabled
        if($ajax != '1'){
            $this->cart->insert($items); //if posted the regular non-ajax way, the fields will be in an array in the correct format
            echo 'false';
            redirect('cart'); // If javascript is not enabled, reload the page with new data
        }else{
    
            $i = 0;
            foreach($items as $key=>$form_field)
            {
                $field_name = $form_field['name'];
                $from_char = strrpos($field_name, '[') +1 ;
                $length = strlen($field_name)-$from_char-1;
                $field = substr($field_name,$from_char,$length);
    
                $data[$i][$field] = $form_field['value'];
                if ($field == "discount_price") $i+=1; // I know 'discount price' is always the last field
            }
    
            $this->cart->insert($data);
    
            echo 'true'; // If javascript is enabled, return true, so the cart gets updated
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-11
      • 2018-02-15
      • 1970-01-01
      • 1970-01-01
      • 2015-02-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多