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

}

Просмотров: передний / 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>

Я не знаю, что делать … На самом деле я сделал 2 просмотра, чтобы добавить корзину в другой вид с именем checkout, но он все еще не добавлен. Итак, я делаю таблицу на той же странице, чтобы увидеть, добавляется ли еда, которую нажимают, в таблицу или нет. И это все еще не может быть добавлено в таблицу.
введите описание изображения здесь

0

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector