2013-02-21 48 views
0

我正在用Codeigniter構建基於ajax的購物車,並且添加/刪除功能完美地工作。我現在試圖添加一個選項來添加多個項目,並不能讓它工作。Codeigniter購物車:如何使用ajax和jquery添加多個項目

這是我正在使用的標記。我不確定它是否是最好的設計,但它與非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,這些項目將被添加到購物車,並且一切正常。那麼普通帖子和ajax帖子有什麼區別呢?

回答

0

嗯,我得到它的工作,但我不知道它是否是最優雅的方式。

這裏就是我所做的:

在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 
    } 
}