简体   繁体   English

我如何通过jquery ajax对html元素进行永久更改,以使其在刷新页面后仍保留?

[英]How do i make changes to an html element through jquery ajax permanent so it remains after refreshing the page?

Im a beginner in codeigniter and am trying to make sort of an ecommerce website. 我是Codeigniter的初学者,正在尝试建立一个电子商务网站。

Now whenever a user clicks on the "Add to cart" button in my view, i use jquery ajax to send a request to a controller function which returns two variables count(total items in cart) and total(the total value of items in cart) as a string and then i modify a div with class .cart to simply display a string containing the two variables. 现在,每当用户单击我视图中的“添加到购物车”按钮时,我都使用jquery ajax向控制器函数发送请求,该函数返回两个变量count(购物车中的商品总数)和total(购物车中商品的总价值) )作为字符串,然后我将.cart类的div修改为仅显示包含两个变量的字符串。

$(document).ready(function(){

    $("#addtocart").click(function(){


    var id = $("#product_id").val();
    var qty = $("#quantity").val(); 

$.ajax({
        url: "<?php echo base_url('cart/addtocart'); ?>",
        data: {id: id , qty:qty },
        type: 'POST',       
        success: function(name) {

            $(".cart").html(name); //This is the div where show number of items in cart and total value of cart

        }
    });

    });
})

But the string doesnt remain when i refresh the page or go to another page. 但是当我刷新页面或转到另一页面时,字符串不会保留。 i want the change made through jquery permanent and remain their regardless where the user navigates in the website. 我希望通过jquery所做的更改永久保留,并且无论用户在网站中的导航位置如何,都保留它们。 I need direction on how to accomplish this. 我需要有关如何完成此操作的指导。 if i need to save it to my database, then what and how to go about it 如果我需要将其保存到数据库中,那么该如何处理

use post method like 使用像

$.post('url',{var:value,var2:value},function(data){
  $('.cart').html(data);
});

$('.cart').html(data); or $('.cart').load(data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM