ajax error 500-internal-server saat update codeigniter cread,read & delete success

permisi kak saya mau tanya saat menggunakan ajax function create,read & delete saya bisa namun saat update tidak bisa

View admin/kategori/list.php


var save_method; //for save method string
var table;
$(document).ready(function() {

    //datatables
    table = $('#kategori').DataTable({

        "processing": true, //Feature control the processing indicator.
        "serverSide": true, //Feature control DataTables' server-side processing mode.
        "order": [], //Initial no order.

        // Load data for the table's content from an Ajax source
        "ajax": {
            "url": "<?php echo site_url('admin/kategori/ajax_list')?>",
            "type": "POST",
        },

        //Set column definition initialisation properties.
        "columnDefs": [
        {
        	"searchable": true, "orderable": true, "targets": [0,1]
        },
        ],

    });

    //set input/textarea/select event when change value, remove class error and remove text help block
    $("input").change(function(){
        $(this).parent().parent().removeClass('has-error');
        $(this).next().empty();
    });
    $("textarea").change(function(){
        $(this).parent().parent().removeClass('has-error');
        $(this).next().empty();
    });
    $("select").change(function(){
        $(this).parent().parent().removeClass('has-error');
        $(this).next().empty();
    });

});

function add_kategori()
{
    save_method = 'add';
    $('#form')[0].reset(); // reset form on modals
    $('.form-group').removeClass('has-error'); // clear error class
    $('.help-block').empty(); // clear error string
    $('#modal_form').modal('show'); // show bootstrap modal
    $('.modal-title').text('Add Kategori'); // Set Title to Bootstrap modal title
}

function edit_kategori(id_kategori)
{
    save_method = 'update';
    $('#form')[0].reset(); // reset form on modals
    $('.form-group').removeClass('has-error'); // clear error class
    $('.help-block').empty(); // clear error string

    //Ajax Load data from ajax
    $.ajax({
        url : "<?php echo site_url('admin/kategori/ajax_edit/')?>/" + id_kategori,
        type: "GET",
        dataType: "JSON",
        success: function(data)
        {

            $('[name="id_kategori"]').val(data.id_kategori);
            $('[name="slug_kategori"]').val(data.slug_kategori);
            $('[name="nama_kategori"]').val(data.nama_kategori);
            $('[name="urutan"]').val(data.urutan);
            $('#modal_form').modal('show'); // show bootstrap modal when complete loaded
            $('.modal-title').text('Edit Person'); // Set title to Bootstrap modal title

        },
        error: function (jqXHR, textStatus, errorThrown)
        {
            alert('Error get data from ajax');
        }
    });
}

function reload_table()
{
    table.ajax.reload(null,false); //reload datatable ajax
}

function save()
{
    $('#btnSave').text('saving...'); //change button text
    $('#btnSave').attr('disabled',true); //set button disable
    var url;

    if(save_method == 'add') {
        url = "<?php echo site_url('admin/kategori/ajax_add')?>";
    } else {
        url = "<?php echo site_url('admin/kategori/ajax_update')?>";
    }

    // ajax adding data to database
     $.ajax({
        url : url,
        type: "POST",
        data: $('#form').serialize(),
        dataType: "JSON",
        success: function(data)
        {

            if(data.status) //if success close modal and reload ajax table
            {
                $('#modal_form').modal('hide');
                reload_table();
            }
            else
            {
                for (var i = 0; i < data.inputerror.length; i++)
                {
                    $('[name="'+data.inputerror[i]+'"]').parent().parent().addClass('has-error'); //select parent twice to select div form-group class and add has-error class
                    $('[name="'+data.inputerror[i]+'"]').next().text(data.error_string[i]); //select span help-block class set text error string
                }
            }
            $('#btnSave').text('save'); //change button text
            $('#btnSave').attr('disabled',false); //set button enable

        },
        error: function (jqXHR, textStatus, errorThrown)
        {
            alert('Error adding / update data');
            $('#btnSave').text('save'); //change button text
            $('#btnSave').attr('disabled',false); //set button enable

        }
    });
}

function delete_kategori(id_kategori)
{
    if(confirm('Are you sure delete this data?'))
    {
        // ajax delete data to database
        $.ajax({
            url : "<?php echo site_url('admin/kategori/ajax_delete')?>/"+id,
            type: "POST",
            dataType: "JSON",
            success: function(data)
            {
                //if success reload ajax table
                $('#modal_form').modal('hide');
                reload_table();
            },
            error: function (jqXHR, textStatus, errorThrown)
            {
                alert('Error deleting data');
            }
        });

    }
}

Controller admin/kategori.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Kategori extends CI_Controller {

	public function __construct()
	{
		parent::__construct();
		$this->load->model('kategori_model','kategori');
		$this->load->library('check_login');
	}

	public function index()
	{
		$kategori = $this->kategori->get_datatables();
		$data = array( 'title' => 'Data kategori (' .!empty($kategori).')',
						'galeri' => $kategori,
						'isi'  => 'admin/kategori/list'
				);
		$this->load->helper('url');
		$this->load->view('admin/layout/wrapper', $data, FALSE);
		//$this->load->view('admin/layout/wrapper');
		//$this->load->view('admin/kategori/list');
	}

	public function ajax_list()
	{
		$list = $this->kategori->get_datatables();
		$data = array();
		$no = $_POST['start'];
		foreach ($list as $kategori) {
			$no++;
			$row = array();
			$row[] = $kategori->id_kategori;
			$row[] = $kategori->slug_kategori;
			$row[] = $kategori->nama_kategori;
			$row[] = $kategori->urutan;

			$row[] = '<a class="btn btn-warning btn-sm" href="javascript:void(0)" title="Edit" onclick="edit_kategori('."'".$kategori->id_kategori."'".')"><i class="fa fa-edit"></i> Edit</a>
				  <a class="btn btn-danger btn-sm" href="javascript:void(0)" title="Hapus" onclick="delete_kategori('."'".$kategori->id_kategori."'".')"><i class="fa fa-trash"></i> Delete</a>';

			$data[] = $row;
		}

		$output = array(
						"draw" => $_POST['draw'],
						"recordsTotal" => $this->kategori->count_all(),
						"recordsFiltered" => $this->kategori->count_filtered(),
						"data" => $data,
				);
		//output to json format
		echo json_encode($output);
	}

	public function ajax_edit($id_kategori)
	{
		$data = $this->kategori->get_by_id($id_kategori);
		echo json_encode($data);
	}

	public function ajax_add()
	{
		$this->_validate();
		$i = $this->input;
		$slug_kategori = url_title($this->input->post('nama_kategori'), 'dash', TRUE);
		$data = array(	'slug_kategori'	=> $slug_kategori,
						'nama_kategori'	=> $i->post('nama_kategori'),
						'urutan' 		=> $i->post('urutan')
						);
		$insert = $this->kategori->save($data);
		echo json_encode(array("status" => TRUE));
	}

	public function ajax_update($id_kategori)
	{
		$this->_validate();
		$i = $this->input;
		$slug_kategori = url_title($this->input->post('nama_kategori'), 'dash', TRUE);
		$data = array(
						'slug_kategori'	=> $slug_kategori,
						'nama_kategori'	=> $i->post('nama_kategori'),
						'urutan' 		=> $i->post('urutan')
					);
		$this->kategori->update(array('id_kategori' => $this->input->post('id_kategori')), $data);
		echo json_encode(array("status" => TRUE));
	}

	public function ajax_delete($id_kategori)
	{
		$this->check_login->check();

		$this->kategori->delete_by_id($id_kategori);
		echo json_encode(array("status" => TRUE));
	}

	private function _validate()
	{
		$data = array();
		$data['error_string'] = array();
		$data['inputerror'] = array();
		$data['status'] = TRUE;

		if($this->input->post('nama_kategori') == '')
		{
			$data['inputerror'][] = 'nama_kategori';
			$data['error_string'][] = 'Nama Kategori Harus Di isi';
			$data['status'] = FALSE;
		}

		if($this->input->post('urutan') == '')
		{
			$data['inputerror'][] = 'urutan';
			$data['error_string'][] = 'Urutan Harus Di isi';
			$data['status'] = FALSE;
		}

		if($data['status'] === FALSE)
		{
			echo json_encode($data);
			exit();
		}
	}

}


Model

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Kategori_model extends CI_Model {

	var $table = 'kategori';
	var $column_order = array('id_kategori','slug_kategori','nama_kategori','urutan','tanggal',null); //set column field database for datatable orderable
	var $column_search = array('id_kategori','slug_kategori','nama_kategori','urutan'); //set column field database for datatable searchable just firstname , lastname , address are searchable
	var $order = array('id_kategori' => 'asc'); // default order

	public function __construct()
	{
		parent::__construct();
		$this->load->database();
	}

	private function _get_datatables_query()
	{
		$this->db->from($this->table);

		$i = 0;


		if(isset($_POST['order'])) // here order processing
		{
			$this->db->order_by($this->column_order[$_POST['order']['0']['column']], $_POST['order']['0']['dir']);
		}
		else if(isset($this->order))
		{
			$order = $this->order;
			$this->db->order_by(key($order), $order[key($order)]);
		}
	}

	function get_datatables()
	{
		$this->_get_datatables_query();
		if($_POST['length'] != -1)
		$this->db->limit($_POST['length'], $_POST['start']);
		$query = $this->db->get();
		return $query->result();
	}

	function count_filtered()
	{
		$this->_get_datatables_query();
		$query = $this->db->get();
		return $query->num_rows();
	}

	public function count_all()
	{
		$this->db->from($this->table);
		return $this->db->count_all_results();
	}

	public function get_by_id($id_kategori)
	{
		$this->db->from($this->table);
		$this->db->where('id_kategori',$id_kategori);
		$query = $this->db->get();

		return $query->row();
	}

	public function save($data)
	{
		$this->db->insert($this->table, $data);
		return $this->db->insert_id();
	}

	public function update($where, $data)
	{
		$this->db->update($this->table, $data, $where);
		return $this->db->affected_rows();
	}

	public function delete_by_id($id_kategori)
	{
		$this->db->where('id_kategori', $id_kategori);
		$this->db->delete($this->table);
	}

}

avatar adamkenway007
@adamkenway007

13 Kontribusi 1 Poin

Dipost 6 tahun yang lalu

Belum ada Jawaban. Jadi yang pertama Jawaban

Login untuk ikut Jawaban