I’m having a problem 500 Internal server error (Ajax Error) that can’t be resolved. I’ve tried to fix it by looking at several references on the internet but haven’t found a solution
model : Madmin.php
this is my Model code
public function add_cart($post) {
$query = $this->db->query("SELECT MAX(id_cart) AS no_cart FROM cart");
if ($query->num_rows() > 0) {
$row = $query->row();
$cart_no = ((int)$row->no_cart) + 1;
} else {
$cart_no = "1";
}
$params = array(
'id_cart' => $cart_no,
'id_barang' => $post['id_barang'],
'harga' => $post['harga'],
'qty' => $post['qty'],
'total' => ($post['harga'] * $post['qty']),
'id_user' => $this->session->userdata('id_user'),
);
log_message('debug', 'Insert parameters: ' . print_r($params, true)); // Logging parameter insert
$this->db->insert('cart', $params);
if ($this->db->affected_rows() > 0) {
log_message('debug', 'Data inserted successfully'); // Logging sukses
} else {
log_message('error', 'Failed to insert data'); // Logging gagal
}
}
javascript : transaksi.php
This is my javascript code
<script>
$(document).on('click', '#select', function() {
$('#id_barang').val($(this).data('id'))
$('#barcode').val($(this).data('barcode'))
$('#harga').val($(this).data('harga'))
$('#stok').val($(this).data('stok'))
$('#modal-item').modal('hide')
});
$(document).on('click', '#add_cart', function() {
var id_barang = $('#id_barang').val();
var harga = $('#harga').val();
var stok = $('#stok').val();
var qty = $('#qty').val();
console.log('Add Cart button clicked');
if (id_barang == '') {
Swal.fire({
icon: 'error',
title: 'Oops...',
text: 'Barang belum dipilih!',
});
$('#barcode').focus();
} else if (stok < 1) {
Swal.fire({
icon: 'error',
title: 'Oops...',
text: 'Stok tidak mencukupi!',
});
$('#id_barang').val('');
$('#barcode').val('');
$('#barcode').focus();
} else {
$.ajax({
type: 'POST',
url: '<?=site_url('transaksi/proses');?>',
data: {
'add_cart': true,
'id_barang': id_barang,
'harga': harga,
'qty': qty
},
dataType: 'JSON',
success: function(result) {
console.log(result);
if (result.success) {
$('#cart_table').load('<?=site_url('transaksi/cart_data')?>', function() {
window.location.reload();
});
} else {
Swal.fire({
icon: 'error',
title: 'Oops...',
text: 'Gagal tambah item cart',
});
}
},
error: function(xhr, status, error) {
console.error('AJAX error', status, error);
Swal.fire({
icon: 'error',
title: 'Oops...',
text: 'Terjadi kesalahan pada server!',
});
}
});
}
});
controller : Transaksi.php
This is my controller code
<?php
defined('BASEPATH') OR exit('no direct script access allowed');
class Transaksi extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('Madmin');
}
public function index(){
if(empty($this->session->userdata('username'))){
redirect('userpanel');
}
$id_user = $this->session->userdata('id_user');
// Memeriksa status pengguna
$user_status = $this->Madmin->get_user_status($id_user);
if ($user_status == 'Aktif') {
$pelanggan = $this->Madmin->get_customer($id_user)->result();
$barang = $this->Madmin->get_barang_category($id_user)->result();
$cart = $this->Madmin->get_cart(null, $id_user);
$data = array(
'pelanggan' => $pelanggan,
'barang' => $barang,
'cart' => $cart,
'invoice' => $this->Madmin->invoice_no(),
);
$data['transaksi']=$this->Madmin->get_all_data('transaksi')->result();
$this->load->view('user/layout/header');
$this->load->view('user/layout/menu');
$this->load->view('user/kasir/transaksi',$data);
$this->load->view('user/layout/footer');
} else {
$this->load->view('demo/layout/header');
$this->load->view('demo/layout/menu');
$this->load->view('demo/dashboard');
$this->load->view('demo/layout/footer');
}
}
public function proses() {
$data = $this->input->post(null, TRUE);
log_message('debug', 'proses function called'); // Logging masuk ke fungsi
log_message('debug', 'Received data: ' . print_r($data, true)); // Logging data yang diterima
if(isset($data['add_cart'])) {
log_message('debug', 'add_cart set'); // Logging jika add_cart diset
$this->Madmin->add_cart($data);
if($this->db->affected_rows() > 0) {
$params = array("success" => true);
} else {
$params = array("success" => false);
}
echo json_encode($params);
} else {
log_message('debug', 'add_cart not set'); // Logging jika add_cart tidak diset
}
if(isset($data['process_payment'])) {
log_message('debug', 'process_payment set'); // Logging jika process_payment diset
$id_transaksi = $this->Madmin->add_sale($data);
$cart = $this->Madmin->get_cart()->result();
$row = [];
foreach($cart as $c => $value){
array_push($row, array(
'id_transaksi' => $id_transaksi,
'id_barang' => $value->id_barang,
'harga' => $value->harga,
'qty' => $value->qty,
'total' => $value->total,
));
}
$this->Madmin->add_sale_detail($row);
$this->Madmin->del_cart(['id_user' => $this->session->userdata('id_user')]);
if($this->db->affected_rows() > 0) {
$params = array("success" => true);
} else {
$params = array("success" => false);
}
echo json_encode($params);
}
}
public function reset_cart() {
$this->Madmin->del_cart(['id_user' => $this->session->userdata('id_user')]);
if ($this->db->affected_rows() > 0) {
$params = array("success" => true);
} else {
$params = array("success" => false);
}
echo json_encode($params);
}
public function update_cart() {
$data = $this->input->post(null, TRUE);
if(isset($_POST['update_cart'])) {
$this->Madmin->update_cart($data);
}
if($this->db->affected_rows() > 0) {
$params = array("success" => true);
} else {
$params = array("success" => false);
}
echo json_encode($params);
}
function cart_data(){
$id_user = $this->session->userdata('id_user');
$cart = $this->Madmin->get_cart(null, $id_user);
$data['cart'] = $cart;
$this->load->view('user/kasir/cart_data',$data);
}
public function delete($id_cart, $params = null) {
if($params != null) {
$this->db->where($params);
}
$this->Madmin->delete_cart_item($id_cart);
redirect('transaksi');
}
}