Codeigniter:无法将商品添加到购物车

时间:2018-11-02 12:14:33

标签: php jquery codeigniter

我单击添加到购物车按钮时,我的添加到购物车功能没有添加食物。

控制器:Asiabuffet.php

class Asiabuffet extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->library('cart');
        $this->load->model('food_model');
    }

    public function index()
    {
        $data['data']=$this->food_model->get_all_food();
        $this->load->view("front/asiabuffet",$data);
    }

    function add_to_cart(){ 
        $data = array(
            'id' => $this->input->post('id_food'), 
            'name' => $this->input->post('judul'), 
            'qty' => $this->input->post('quantity'), 
        );
        $this->cart->insert($data);
        echo $this->show_cart(); 
    }

    function show_cart(){ 
        $output = '';
        $no = 0;
        foreach ($this->cart->contents() as $items) {
            $no++;
            $output .='
                <tr>
                    <td>'.$items['name'].'</td>
                    <td>'.$items['qty'].'</td>
                    <td><button type="button" id="'.$items['rowid'].'" class="remove_cart btn btn-danger btn-sm">Cancel</button></td>
                </tr>
            ';
        }
        return $output;
    }

    function load_cart(){ 
        echo $this->show_cart();
    }

    function delete_cart(){ 
        $data = array(
            'rowid' => $this->input->post('row_id'), 
            'qty' => 0, 
        );
        $this->cart->update($data);
        echo $this->show_cart();
    }
}

型号:Food_model.php

class Food_model extends CI_Model{

    function get_all_food(){
        $result=$this->db->get('food');
        return $result;
    }

}

查看次数:front / asiabuffet.php

<div class="col-md-12">
                    <?php foreach ($data->result() as $row) : ?>
                    <table class="table table-form">
                        <tbody>
                            <tr>
                                <td><?php echo $row->judul;?></td>
                                <td>
                                    <div class="form-group">
                                        <div class="col-md-2"></div>
                                        <div class="col-md-8">
                                            <input type="number" name="quantity" id="<?php echo $row->id_food;?>" value="0" class="quantity form-control">
                                        </div>
                                        <div class="col-md-2"></div>

                                    </div>
                                </td>
                                <td>
                                    <div class="col-md-12 text-center">
                                        <button class="btn btn-primary btn-submit-card" data-id_food="<?php echo $row->id_food;?>" data-judul="<?php echo $row->judul;?>" onclick="add_cart(<?php echo $row->id_food;?>)">Add To Cart</button>
                                    </div>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                    <?php endforeach;?>
                </div>
                <div class="col-md-4">
                    <h4>Shopping Cart</h4>
                    <table class="table table-striped">
                        <thead>
                            <tr>
                                <th>Items</th>
                                <th>Qty</th>
                                <th>Actions</th>
                            </tr>
                        </thead>
                        <tbody id="detail_cart">
                        </tbody>
                    </table>

视图中的jQuery

<script type="text/javascript">
            $(document).ready(function(){
                $('.add_cart').click(function(){
                    var id_food  = $(this).data("id_food");
                    var judul    = $(this).data("judul");
                    var quantity = $('#' + id_food).val();
                    $.ajax({
                        url : "<?php echo site_url('asiabuffet/add_to_cart');?>",
                        method : "POST",
                        data : {id_food: id_food, judul: judul, quantity: quantity},
                        success: function(data){
                            $('#detail_cart').html(data);
                        }
                    });
                });

                $('#detail_cart').load("<?php echo site_url('asiabuffet/load_cart');?>");

                $(document).on('click','.remove_cart',function(){
                    var row_id=$(this).attr("id"); 
                    $.ajax({
                        url : "<?php echo site_url('asiabuffet/delete_cart');?>",
                        method : "POST",
                        data : {row_id : row_id},
                        success :function(data){
                            $('#detail_cart').html(data);
                        }
                    });
                });
            });
        </script>

我不知道该怎么办...实际上,我在将购物车添加到名为checkout的其他视图中有2个视图,但仍未添加。因此,我在同一页上制作表格,以查看所单击的食物是否添加到表格中。而且它仍然不能添加到表中。 enter image description here

0 个答案:

没有答案