im already wrote some code for dropdown with onchange event but the problem is when i refresh it, it doesnt show the data but after i select different value in dropd down its show the data
View
Code:
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('#divTahunAjar select').change(function () {
var selState = this.value; //value from dropdown tahun ajar
console.log(selState);
$.ajax({
url: "<?php echo base_url(); ?>controllers/getDataSekolah/", //The url where the server req would we made.
async: false,
type: "POST", //The type which you want to use: GET/POST
data: "state="+selState, //The variables which are going.
dataType: "html", //Return data type (what we expect).
//This is the function which will be called if ajax call is successful.
success: function(data)
{
//data is the html of the page where the request is made.
$('#result').html(data);
}
})
});
});
</script>
Code:
<div id="divTahunAjar">
<?php
$attributes = 'class="form-control"';
echo form_dropdown('tahunAjar', $dropDownTahunAjar, '#', $attributes);
?>
</div>
<div id="result">
</div>
Controllers
Code:
function getDataSekolah()
{
$tahunAjar_id=$_POST['state'];
$result=$this->mainModels->getDataSekolah($tahunAjar_id);
print "<br/>";
foreach($result as $row)
{
echo "<li><a href=".base_url()."controllers/processSekolah/".$row->kd_sekolah.">".$row->nama_sekolah." >> ".$row->kota."</a></li>";
}
}
Model
Code:
function getDataSekolah($tahunAjar_id)
{
$this->db->select('*');
$this->db->where('kd_thn_ajar', $tahunAjar_id);
$this->db->join('kepsek','kepsek.kd_kepsek=sekolah.kd_kepsek','left');
$this->db->join('kota','kota.kd_kota=sekolah.kd_kota','left');
$query = $this->db->get('sekolah');
return $query->result();
}