controller nya
========================
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Wilayah extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->model('model_wilayah');
}
public function index() {
$data['provinsi'] = $this->model_wilayah->get_provinsi();
$this->load->view('wilayah', $data);
}
public function data_kabupaten() {
$data = $this->model_wilayah->get_kabupaten();
foreach ($data->result() as $d) {
echo "<option value=$d->kabupaten_id>$d->nama_kabupaten</option>";
}
}
public function data_kecamatan() {
$data = $this->model_wilayah->get_kecamatan();
foreach ($data->result() as $k) {
echo "<option value=$k->kecamatan_id>$k->nama_kecamatan</option>";
}
}
public function test(){
$data['tes'] = "test";
$this->load->view('test', $data);
}
}
=======================================
model nya
=======================================
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Model_wilayah extends CI_Model {
public function get_provinsi() {
return $this->db->get('provinsi');
}
public function get_kabupaten() {
$provinsi_id = $this->input->get('provinsi_id');
$this->db->where('provinsi_id', $provinsi_id);
return $this->db->get('kabupaten');
}
public function get_kecamatan() {
$kabupaten_id = $this->input->get('kabupaten_id');
$this->db->where('kabupaten_id', $kabupaten_id);
return $this->db->get('kecamatan');
}
}
=======================================
<!DOCTYPE html>
<html>
<head>
<title>DaengWeb - Data Wilayah</title>
<script src="<?php echo base_url('assets/js/jquery-2.1.4.min.js'); ?>"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#provinsi").change(function(){
var provinsi_id = $("#provinsi").val();
$.ajax({
type: "GET",
url: "<?php echo site_url('wilayah/data_kabupaten'); ?>",
data: "provinsi_id="+provinsi_id,
success: function(html) {
$("#kabupaten").html(html);
}
});
});
});
</script>
<script type="text/javascript">
$(document).ready(function(){
$("#kabupaten").change(function(){
var kabupaten_id = $("#kabupaten").val();
$.ajax({
type: "GET",
url: "<?php echo site_url('wilayah/data_kecamatan'); ?>",
data: "kabupaten_id="+kabupaten_id,
success: function(html) {
$("#kecamatan").html(html);
}
});
});
});
</script>
</head>
<body>
Pilih Provinsi :
<select id="provinsi" onchange="data_provinsi">
<option value="0">Silahkan Pilih</option>
<?php
foreach ($provinsi->result() as $p) {
?>
<option value="<?php echo $p->provinsi_id; ?>"><?php echo $p->nama_provinsi; ?></option>
<?php } ?>
</select> <br>
Pilih Kabupaten :
<select id="kabupaten"></select> <br>
Pilih Kecamatan :
<select id="kecamatan"></select>
</body>
</html>
============================]
selamat mencoba !!!
Langganan:
Posting Komentar (Atom)
0 Response to "combo cox dinamis codeigniter"
Posting Komentar