I have a shopping cart in Codeigniter in which products are added to the cart using (jQuery) Ajax. In the header of each page it displays the number of contents in the cart using
我在Codeigniter有一个购物车,使用(jQuery)Ajax将产品添加到购物车中。在每个页面的标题中,它显示购物车中使用的内容数量
<?php echo $this->cart->total_items(); ?>
How can I update/refresh this variable when a product is added to the cart, as I am using AJAX to add products to the cart and thus the page isn't being reloaded when products are added. It seems pointless to use AJAX to do asyncronous stuff if I have to reload the page to get the new number of total items from the server.
如何在将产品添加到购物车时更新/刷新此变量,因为我使用AJAX将产品添加到购物车,因此在添加产品时不会重新加载页面。如果我必须重新加载页面以从服务器获取新的总项目数,那么使用AJAX做异步的东西似乎毫无意义。
Many thanks.
非常感谢。
4 个解决方案
#1
1
You should be able to do this by making the element containing the Shopping Card Item Count easily addressable by JavaScript, so:
您应该可以通过使用JavaScript轻松解决包含购物卡项目计数的元素来实现此目的,因此:
<span id="total_items"><?php echo $this->cart->total_items(); ?></span>
Then, as part of the AJAX Function you perform, have the response contain the revised value for the total items, and then have your function replace the content of this span with that value.
然后,作为您执行的AJAX函数的一部分,让响应包含总项的修订值,然后让您的函数用该值替换此span的内容。
#2
0
As I understood you, you need something to dynamically update your number of articles ?
据我了解,您需要动态更新文章数量吗?
You can use jQuery PeriodicalUpdater to update that number "every once in a while" ( point it to a script that returns the current number only, ofc ).
您可以使用jQuery PeriodicalUpdater“每隔一段时间”更新一次该数字(将其指向仅返回当前数字的脚本,ofc)。
#3
0
Don't think about PHP in this case — it's totally irrelevant. What you need to do is modify content of that element client-side. So if you have:
在这种情况下不要考虑PHP - 它完全无关紧要。您需要做的是修改该元素客户端的内容。所以如果你有:
<div id="total_items"><?php /* ... */ ?></div>
Then in your AJAX success handler you should do something like:
然后在您的AJAX成功处理程序中,您应该执行以下操作:
$('#total_items').html('new item count');
#4
0
First, you can update as many parts of the page as you want when you make a jQuery call. What is the format of the data that is being returned from the call? Lets say you are returning JSON, then the JSON string should include the total. Then in the callback of the AJAX call you can simply update the total count.
首先,您可以在进行jQuery调用时根据需要更新页面的多个部分。从通话中返回的数据格式是什么?假设您正在返回JSON,那么JSON字符串应该包含总数。然后在AJAX调用的回调中,您可以简单地更新总计数。
#1
1
You should be able to do this by making the element containing the Shopping Card Item Count easily addressable by JavaScript, so:
您应该可以通过使用JavaScript轻松解决包含购物卡项目计数的元素来实现此目的,因此:
<span id="total_items"><?php echo $this->cart->total_items(); ?></span>
Then, as part of the AJAX Function you perform, have the response contain the revised value for the total items, and then have your function replace the content of this span with that value.
然后,作为您执行的AJAX函数的一部分,让响应包含总项的修订值,然后让您的函数用该值替换此span的内容。
#2
0
As I understood you, you need something to dynamically update your number of articles ?
据我了解,您需要动态更新文章数量吗?
You can use jQuery PeriodicalUpdater to update that number "every once in a while" ( point it to a script that returns the current number only, ofc ).
您可以使用jQuery PeriodicalUpdater“每隔一段时间”更新一次该数字(将其指向仅返回当前数字的脚本,ofc)。
#3
0
Don't think about PHP in this case — it's totally irrelevant. What you need to do is modify content of that element client-side. So if you have:
在这种情况下不要考虑PHP - 它完全无关紧要。您需要做的是修改该元素客户端的内容。所以如果你有:
<div id="total_items"><?php /* ... */ ?></div>
Then in your AJAX success handler you should do something like:
然后在您的AJAX成功处理程序中,您应该执行以下操作:
$('#total_items').html('new item count');
#4
0
First, you can update as many parts of the page as you want when you make a jQuery call. What is the format of the data that is being returned from the call? Lets say you are returning JSON, then the JSON string should include the total. Then in the callback of the AJAX call you can simply update the total count.
首先,您可以在进行jQuery调用时根据需要更新页面的多个部分。从通话中返回的数据格式是什么?假设您正在返回JSON,那么JSON字符串应该包含总数。然后在AJAX调用的回调中,您可以简单地更新总计数。