CodeIgniter Laravel PHP Example Javascript jQuery MORE Videos New

How to delete data from database using CodeIgniter Ajax


For delete record we use 3 file here

view.php(View)

<!DOCTYPE html>
<html lang="en">
<head>
  <title>View Ajax</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
  <h2>View data</h2>
	<table class="table table-bordered table-sm" >
    <thead>
      <tr>
		<th>Sl No</th>
        <th>Name</th>
        <th>Email</th>
        <th>Phone</th>
		<th>City</th>
		<th>Action</th>
      </tr>
    </thead>
    <tbody id="table">
      
    </tbody>
  </table>
</div>
<script>
$(document).ready(function() {
	$.ajax({
		url: "<?php echo base_url("Crud/viewajax");?>",
		type: "POST",
		cache: false,
		success: function(dataResult){
			$('#table').html(dataResult); 
		}
	});
	$(document).on("click", ".delete", function() { 
	//alert("Success");
		var $ele = $(this).parent().parent();
		$.ajax({
			url: "<?php echo base_url("Crud/deleterecords");?>",
			type: "POST",
			cache: false,
			data:{
				type: 2,
				id: $(this).attr("data-id")
			},
			success: function(dataResult){
				alert(dataResult);
				var dataResult = JSON.parse(dataResult);
				if(dataResult.statusCode==200){
					$ele.fadeOut().remove();
				}
			}
		});
	});
});
</script>
</body>
</html>

Crud.php(Controller)

<?php
class Crud extends CI_Controller 
{
	public function __construct()
	{
		parent::__construct();
		$this->load->helper('url'); 
		$this->load->database();
		$this->load->model('Crud_model');
	}
	
	public function view()
	{
		$this->load->view('view');
	}
	function deleterecords()
	{
		if($this->input->post('type')==2)
		{
			$id=$this->input->post('id');
			$this->Crud_model->deleterecords($id);	
			echo json_encode(array(
				"statusCode"=>200
			));
		} 
	}
	public function viewajax()
	{
				$data=$this->Crud_model->display_records();
				$i=1;
				foreach($data as $row)
				{
					  echo "<tr>";
					  echo "<td>".$i."</td>";
					  echo "<td>".$row->name."</td>";
					  echo "<td>".$row->email."</td>";
					  echo "<td>".$row->phone."</td>";
					  echo "<td>".$row->city."</td>";
					  echo "<td><button type='button' class='btn btn-danger btn-sm delete' data-id='".$row->id."'>Delete</button></td>";
					  echo "</tr>";
					  $i++;
				}
	}
}

Crud_model.php(Model)

<?php
class Crud_model extends CI_Model 
{
	
	function display_records()
	{
		$query=$this->db->query("select * from crud");
		return $query->result();
	}
	function deleterecords($id)
	{
		$query="DELETE FROM `crud` WHERE id=$id";
		$this->db->query($query);
	}
}