Codeigniter

Autocomplete text with Codeigniter, JQuery and JSON

  • Oleh admin
  • 3 menit baca
  • Belum ada komentar
First step :  Create Database with the name autocomplete_db.
  
Second step : Download from this Link To Download Database.  And import to your database.
Third step : Open database.php in the application\config\ and change this script according your username, password and database :
$db[‘default’][‘hostname’] = ‘localhost‘;
$db[‘default’][‘username’] = ‘root‘;
$db[‘default’][‘password’] = ‘12345678‘;
$db[‘default’][‘database’] = ‘autocomplete_db‘;
Fourth Step : Open welcome.php in the application\controller\ folder, and copy this script :

<?php if ( ! defined(‘BASEPATH’)) exit(‘No direct script access allowed’);

class Welcome extends CI_Controller {

public function __construct()
{
parent::__construct();
$this->load->model(‘ac_model’);
$this->load->database();
$this->load->helper(‘url’);
}
public function index()
{
$this->load->view(‘ac_view’);
}
public function suggestions()
{
$country = $this->input->post(‘autocomplete’,TRUE);
$rows = $this->ac_model->getData($country);
$json_array = array();
foreach ($rows as $row)
$json_array[]=$row->country;
echo json_encode($json_array);
}
}

Fifth Step : Create ac_model.php in the application\model\ folder, and copy this script :

<?php

class Ac_Model extends CI_Model
{
public function __construct()
{
parent::__construct();
}
public function getData($country)
{
$this->db->select(‘country’);
$this->db->like(‘country’, $country);
$query = $this->db->get(‘country_tb’);
return $query->result();
}
}

Sixth Step : Create ac_view.php in the application\view\ folder, and copy this script :
<link href=”http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css” rel=”stylesheet” type=”text/css”/>
<script type=”text/javascript” src=”http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js”></script>
<script type=”text/javascript” src=”http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js”></script>
<script type=”text/javascript”>
// <![CDATA[
$(document).ready(function () {
$(function () {
$( “#autocomplete” ).autocomplete({
source: function(request, response) {
$.ajax({
url: “<?php echo site_url(‘welcome/suggestions’); ?>”,
data: { country: $(“#autocomplete”).val()},
dataType: “json”,
type: “POST”,
success: function(data){
response(data);
}
});
},
});
});
});
// ]]>
</script>
</head>
<body>
<div id=”container”><div id=”body”>
Text: <input type=”text” id=”autocomplete” />
</div>

</div>
</body>
</html>

Don’t forget to change Base Site URL in application/config/config.php to yourself URL : 
$config[‘base_url’] = ‘//localhost/json_autocomplete_codeigniter/‘;
And this is the result likes image below :

autocomplete

Bagikan

Tentang Penulis

admin

Butuh Bantuan Mengembangkan Website atau Aplikasi?

Diskusikan kebutuhan proyek Anda dan tentukan solusi yang paling sesuai.

0 comments on Autocomplete text with Codeigniter, JQuery and JSON

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *