cart->insert(); not working in CI

1.5k views Asked by At

I check with all stack Sites and google it also. But i can't find proper solution for this.

Any one with idea about this??

Question is data not get insert to cart. I echo data inside controller data retrieving to controller well. But not get inserting to cart??

Controller

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

    class Cart extends CI_Controller
    {    
        public function  __construct()
        {
            parent::__construct();
            $this->load->library('session');
            $this->load->model('Product_Model');
            $this->load->library('cart');

        }    
        public function index()
        {

        }
        public function insert_cart()
        {    
            $data = array(
                'id' => $this->input->post('id'),
                'qty' => $this->input->post('qty'),
                'price' => $this->input->post('price'),
                'name' => $this->input->post('head'),
            );    
            $cartdata = $this->cart->insert($data);

            if(!empty($cartdata)){
                print_r($cartdata);
            }else{
            echo "no data";
        }   

//            $count = $this->cart->contents();
//            print_r($count) ;
            die();
        }    
    }

in view ajex data sending.

<script>
    $(function(){
        $( "#submit" ).click(function(event) {
            event.preventDefault();

            var head= $("#head").text();
            var price= $("#price_value").val();
            var pid= $("#pid").text();
            var qty= $("#qty").val();
//         alert(price);


            $.ajax({
                type:"post",
                url: "<?php echo base_url(); ?>index.php/cart/insert_cart",
                data:{
                    head:head, price:price,pid:pid,qty:qty
                }

            });
        });
    });
</script>
1

There are 1 answers

0
Saty On BEST ANSWER

Problem is with id in your ajax request you pass pid

head:head, price:price,pid:pid,qty:qty
                       ^^^^^^^^

So you receive pid instead of id

'id' => $this->input->post('pid'),// here receive pid not id