Muhammad Muqorrobin Muhammad Muqorrobin - 3 months ago 20
MySQL Question

related select option from database using codeigniter

i have 2 tables in database

this is my tables :
my table from database and the relation

this is my controller

public function add_form(){
$login = $this->session->userdata('logged_in');
$kab = $this->m_lokasi->select_kab();
$prov = $this->m_lokasi->select_prov();
$max = $this->m_lokasi->select_max();
$conf = array(
'max' => $max,
'prov' => $prov


this is my model

public function select_all(){

$this->db->join('provinsi', 'provinsi.IDProvinsi = lokasi_wisata.IDProvinsi');
$this->db->join('kabupaten', 'kabupaten.IDKabupaten = lokasi_wisata.IDKabupaten');
return $this->db->get('lokasi_wisata')->result_object();

public function select_prov(){


return $this->db->get('provinsi')->result_object();
public function select_kab(){
$this->db->join('provinsi', 'provinsi.IDProvinsi = provinsi.IDProvinsi');
return $this->db->get('kabupaten')->result_array();

and this is my view

code for javascript :

function populate(s1,s2){
var s1 = document.getElementById(s1);
var s2 = document.getElementById(s2);
s2.innerHTML = "";
var optionArray = ["<?php echo $value->nama_k?>"];
for(var option in optionArray){
var pair = optionArray[option].split("|");
var newOption = document.createElement("option");
newOption.value = pair[0];
newOption.innerHTML = pair[1];

} </script>

code for select option :

<div class="form-group">
<label class="col-sm-2 control-label">Provinsi</label>
<div class="col-sm-6">
<select name="provinsi" id="sel1" type="text" class="form-control" onchange="populate(,'sel2')">
<<option value=""> </option>}

foreach($prov as $value){ ?>
<option value = "<?php echo $value->IDProvinsi; ?>"><?php echo $value->nama_p ?></option>";
<?php }

<div class="form-group">
<label class="col-sm-2 control-label">Kabupaten</label>
<div class="col-sm-6">
<select name="level" id="sel2" type="text" class="form-control">

iam asking how to show field nama_k from table kabupaten when IDprovinsi onChange ? and how to get value on this code to the controller

var optionArray = ["<?php echo $value->nama_k?>"];

thx stackoverflow


you need an ajax request if I understand well your need

  1. bind the onchange listener to the select field.

  2. in the onchange event handler send an ajax request puting as parameter the IDprovinsi selected and bind an oncomplete event handler to specify the logic to apply when the request is done (i.e add an option to the select field with the value of the nama_k field ). Take care as well to specify the cookie session in the header of the request if access of the view is restricted.

  3. add a method in the controller to filter result from the DB by the IDprovinsi sent from the ajax request and echo the nama_k field value in the standard output (JSON exchange format is recommended for client/server communication)


on client side

$('#sel1').bind('change', function(evt){

    var IDProvinsi = $(this).val();

    var url = "http://localhost/index.php/filter_by_id/" + IDProvinsi;

    $.getJSON(url, function(data){
        // data contains the result of the db request
        // custom logic here: populate the s2 field
    }).fail(function() {
        console.log( "error" );

on server side

public function filter_by_id($IDProvinsi)
    $result = $this->m_lokasi->select_by_provinsi_id($IDProvinsi);

        header("HTTP/1.1 200 OK");
        echo json_encode($result);
        header("HTTP/1.1 404 Not Found"); // or a custom code