2011-10-10 45 views
1

我已經實現從購物車中/ jQuery的AJAX教程的這個codeigniter cart and jquery from nettuts+Ajax調用不僅在IE

它的工作原理妙在所有瀏覽器,除了IE..I'm認爲它有一個CSS選擇這樣做也許早期版本的IE不支持。它不會做的就是加入購物車,就像它假設的那樣。我知道帖子是成功的,但.get和返回的數據加載到div不是。我知道這個帖子正在工作,因爲如果你點擊更新購物車按鈕,它會顯示所有添加的項目,Ajax應該添加和更新沒有頁面重新加載的列表。

這裏是我的JS

$(document).ready(function() { 
    /*place jQuery actions here*/ 
    var link = ""; 


    $("ul.products form").submit(function() { 
     // Get the product ID and the quantity 
     var id = $(this).find('input[name=product_id]').val(); 
     var qty = $(this).find('input[name=quantity]').val(); 

     $.post(link + "cart/add_cart_item", { product_id: id, quantity: qty, ajax: '1' }, 
      function(data){ 

      if(data == 'true'){ 

       $.get(link + "cart/show_cart", function(cart){ 
        $("#cart_content").html(cart); 
       }); 

      }else{ 
       alert("Product does not exist"); 
      } 

     }); 

     return false; 
    }); 

    $(".empty").live("click", function(){ 
     $.get(link + "cart/empty_cart", function(){ 
      $.get(link + "cart/show_cart", function(cart){ 
       $("#cart_content").html(cart); 
      }); 
     }); 

     return false; 
    }); 




}); 

這裏是處理PHP的Ajax調用::

<?php 

class Cart extends MX_Controller { // Our Cart class extends the hmvc (MX)Controller class 

function __construct() 
    { 
     parent::__construct(); // We define the the Controller class is the parent. 
     $this->load->model('cart_model'); // Load our cart model for our entire class 
    } 

    function index() 
    { 
     $data['products'] = $this->cart_model->retrieve_products(); // Retrieve an array with all products 

     $data['content'] = 'cart/cart/products'; // Select view to display 
     $this->load->view('index', $data); // Display the page 
    } 

    function add_cart_item(){ 

     if($this->cart_model->validate_add_cart_item() == TRUE){ 

      // Check if user has javascript enabled 
      if($this->input->post('ajax') != '1'){ 
       redirect('cart'); // 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 
      } 
     } 

    } 

    function update_cart(){ 
     $this->cart_model->validate_update_cart(); 
     redirect('cart'); 
    } 

    function show_cart(){ 
     $this->load->view('cart/cart/cart.php'); 
    } 

    function empty_cart(){ 
     $this->cart->destroy(); 
     redirect('cart'); 
    } 
    function checkout(){ 
    $numitems=$this->cart->total_items(); 

    if($numitems>1){ 
    //more then 1 item 
    $this->load->library('Paypal_Lib'); 
    $multiproductarray=$this->cart->contents(); 
    //echo var_dump($multiproductarray); return; 
    $this->paypal_lib->add_field('business', $this->config->item('paypal_email')); 
      $this->paypal_lib->add_field('return', site_url('paypal/success')); 
      $this->paypal_lib->add_field('cancel_return', site_url('paypal/cancel')); 
      $this->paypal_lib->add_field('notify_url', site_url('paypal/ipn')); // <-- IPN url 
      $this->paypal_lib->multi_items('true'); 
      $i=1; // keeps track for _number 
      foreach($this->cart->contents() as $items){ 
      $this->paypal_lib->add_field('item_name_'.$i, $items['name']); 
     $this->paypal_lib->add_field('item_number_'.$i, $items['id']); 
      $this->paypal_lib->add_field('amount_'.$i, $items['price']); 
      $this->paypal_lib->add_field('quantity_'.$i, $items['qty']); 
     // $this->paypal_lib->add_field('quantity_'.$i, '10'); 
      $i++; 
      } 
      redirect($this->paypal_lib->paypal_get_request_link());//this sends to paypal 


    }else{ 
    //1 item 
     $this->load->library('Paypal_Lib'); 
     //$singleproductarray=$this->cart->contents(); 
     //echo var_dump($singleproductarray); return; 
     //echo $singleproductarray['name'].$singleproductarray['id'].$singleproductarray['price']; return; 
      $this->paypal_lib->add_field('business', $this->config->item('paypal_email')); 
      $this->paypal_lib->add_field('return', site_url('paypal/success')); 
      $this->paypal_lib->add_field('cancel_return', site_url('paypal/cancel')); 
      $this->paypal_lib->add_field('notify_url', site_url('paypal/ipn')); // <-- IPN url 
      $this->paypal_lib->multi_items('false'); 
      //$this->paypal_lib->add_field('item_name', $singleproductarray['name']); 
     // /$this->paypal_lib->add_field('item_number', $singleproductarray['id']); 
      //$this->paypal_lib->add_field('amount', $singleproductarray['price']); 
       foreach($this->cart->contents() as $items){ 
        $this->paypal_lib->add_field('item_name', $items['name']); 
     $this->paypal_lib->add_field('item_number', $items['id']); 
      $this->paypal_lib->add_field('amount', $items['price']); 
        $this->paypal_lib->add_field('quantity', $items['qty']); 

      } 
      redirect($this->paypal_lib->paypal_get_request_link());//this sends to paypal 

    } 

    } 
} 

/* End of file cart.php */ 
/* Location: ./application/controllers/cart.php */ 




<?php 

class Cart_model extends CI_Model { 

    // Function to retrieve an array with all product information 
    function retrieve_products(){ 
     $query = $this->db->get('products'); 
     return $query->result_array(); 
    } 

    // Updated the shopping cart 
    function validate_update_cart(){ 

     // Get the total number of items in cart 
     $total = $this->cart->total_items(); 

     // Retrieve the posted information 
     $item = $this->input->post('rowid'); 
     $qty = $this->input->post('qty'); 

     // Cycle true all items and update them 
     for($i=0;$i < $total;$i++) 
     { 
      // Create an array with the products rowid's and quantities. 
      $data = array(
       'rowid' => $item[$i], 
       'qty' => $qty[$i] 
      ); 

      // Update the cart with the new information 
      $this->cart->update($data); 
     } 

    } 

    // Add an item to the cart 
    function validate_add_cart_item(){ 

     $id = $this->input->post('product_id'); // Assign posted product_id to $id 
     $cty = $this->input->post('quantity'); // Assign posted quantity to $cty 

     $this->db->where('id', $id); // Select where id matches the posted id 
     $query = $this->db->get('products', 1); // Select the products where a match is found and limit the query by 1 

     // Check if a row has been found 
     if($query->num_rows > 0){ 

      foreach ($query->result() as $row) 
      { 
       $data = array(
        'id'  => $id, 
        'qty'  => $cty, 
        'price' => $row->price, 
        'name' => $row->name 
       ); 

       $this->cart->insert($data); 

       return TRUE; 
      } 

     // Nothing found! Return FALSE! 
     }else{ 
      return FALSE; 
     } 
    } 

    // Needed? 
    //function cart_content(){ 
    // return $this->cart->total(); 
    //} 

} 


/* End of file cart_model.php */ 
/* Location: ./application/models/cart_model.php */ 

請告訴我發生的是第一項被加載到購物車中(通過Ajax後,和成功),它會繼續添加項目,但除非我手動按下更新購物車按鈕,否則ajax將不會更新屏幕上的ul列表項目。問題只存在於IE

謝謝!

+1

當我收到它們時,我會接受答案 –

+1

當您顯示基本的調試工作時,您會收到答案。需要花費10秒鐘的時間來粘貼'alert()'來判斷你的代碼是否被調用。再花一分鐘看看你的代碼是否在複雜的ajax範圍之外工作。你既沒有做過,但你期待答案。 –

+0

不靠近電腦..可能不是這個問題,但通常我發佈所有的代碼。沒有看到警報如何提供幫助或將它們放在哪裏,因爲這可以在所有其他瀏覽器中使用。不知道什麼是不正確的ie..ill更新tomm其在這裏遲 –

回答

1

你這是從服務器呼應

echo true ;那麼你應該使用if(data == true)

echo 'true' ;那麼你應該使用if(data == 'true')

echo 1;那麼你應該使用if($.trim(data) == 1)

嘗試用$.trim(data)代替data

希望這可能會有所幫助。