Tutorial Codeigniter 3 Make Auto Complete Using Ajax and Database #Part 1

Make Auto Complete Using Ajax and Database

membuat form auto select combobox di codeigniter dengan library jquery.
1. File Controller Buat Sebuah File Di controller dengan nama c_autocomplete.php
  
<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class C_autocomplete extends CI_Controller {

  public function __construct()
  {
    parent::__construct();
    $this->load->model('m_auto');
  }
  var $title = 'autocomplete';
  public function index()
  { 
    $data['title'] = $this->title;
    $this->load->view('v_autocomplete', $data);
  }

  function get_autocomplete()
  {
    if (isset($_GET['term'])) {
      $result = $this->m_auto->get_prov($_GET['term']);
      if (count($result) > 0) {
        foreach ($result as $row)
        $result_array[] = $row->nama_prov
          );
        echo json_encode($result_array);
      }
    }
  }

}

/* End of file c_autocomplete.php */
/* Location: ./application/controllers/c_autocomplete.php */

2. Model
Buat Sebuah File Di Model dengan nama M_auto.php

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

class M_auto extends CI_Model {

  public function get_prov($title)
  {
    $this->db->like('nama_prov', $title, 'BOTH');
    $this->db->order_by('id_prov', 'asc');
    $this->db->limit(10);
    return $this->db->get('prov')->result();
  }

}

/* End of file m_auto.php */
/* Location: ./application/models/m_auto.php */


3. View
Terakhir Anda Buat File Di Folder View dengan nama v_autocomplete.php
  
<?php include 'template/header.php'; ?>
<body>

<?php include 'template/nav_header.php'; ?>
  <div class="container">
    <h1 align="center">Auto Komplete</h1>
    <div class="col-md-4" style="margin: 0 auto;">
      <label class="control-label">Nim Mahasiswa</label>
      <input type="text" name="title" id="title" placeholder="Masukan Nim Mahasiswa" class="form-control">
      <label class="control-label">Nama Mahasiswa</label>
      <input type="text" name="nama_mahasiswa" placeholder="Nama Mahasiswa" class="form-control">
    </div>
  </div>
<?php include 'template/mahasiswa_input_modal.php'; ?>
</body>
<?php include 'template/footer.php'; ?>

<script>
  $(document).ready(function()
  {
    $("#title").autocomplete({
      source : "<?php echo site_url('C_autocomplete/get_autocomplete') ?>"
      }
    });
});
</script>

Subscribe to receive free email updates:

0 Response to "Tutorial Codeigniter 3 Make Auto Complete Using Ajax and Database #Part 1"

Post a Comment