【发布时间】:2011-10-10 03:11:44
【问题描述】:
我已经从 codeigniter cart and jquery from nettuts+ 实现了购物车/jquery ajax 教程
它在除 IE 之外的所有浏览器中都能正常工作。我认为这与可能早期版本的 IE 不支持的 css 选择器有关。它没有做的是 ajaxify 将添加到购物车中,就像它假设的那样。我知道帖子是成功的,但是 .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;
});
});
这里是ajax调用的处理php::
<?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中
谢谢!
【问题讨论】:
-
收到答案时不接受
-
当您展示基本的调试工作时,您将收到答案。粘贴几个
alert()需要 10 秒才能确定您的代码是否被调用。还需要一分钟来查看您的代码是否在复杂的 ajax 范围之外工作。你什么都没做,但你期待答案。 -
not near pc ..也许不是这个问题,但通常我会发布所有代码。看不到警报将如何提供帮助或将它们放在哪里,因为这在所有其他浏览器中都有效。不知道什么是不正常的 ie..我更新到这里很晚了
-
为了确定你首先需要弄清楚什么是“它”。这是通过将问题缩小到特定行来完成的。但是,如果您只想坐在那里等待某人查看一堆代码并指出其中有什么问题,那很好。
-
在没有错误弹出的情况下如何缩小范围,它在某些版本的 IE 中不起作用。现在是早上(我最初发布时是深夜)所以我要尝试建议然后我会报告回来。你的建议没有帮助。 $("#cart_content").html(cart);确实有效
标签: jquery ajax internet-explorer codeigniter shopping-cart