douchongbc72264 2018-11-02 12:14
浏览 45

Codeigniter:将商品添加到购物车不起作用

My function add to cart didn't adding a food when i click button add to cart.

Controller: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();
    }
}

Models:Food_model.php

class Food_model extends CI_Model{

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

}

Views: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 on view

<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>

I don't know what to do... Actually I made 2 view to adding cart to other view named checkout but it still not added. So, I make table on the same page to see if the food that being clicked adding to table or not. And it still cannot added to table. enter image description here

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 多址通信方式的抗噪声性能和系统容量对比
    • ¥15 winform的chart曲线生成时有凸起
    • ¥15 msix packaging tool打包问题
    • ¥15 finalshell节点的搭建代码和那个端口代码教程
    • ¥15 Centos / PETSc / PETGEM
    • ¥15 centos7.9 IPv6端口telnet和端口监控问题
    • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
    • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
    • ¥20 海浪数据 南海地区海况数据,波浪数据
    • ¥20 软件测试决策法疑问求解答