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.