[eluser]Unknown[/eluser]
I'm making a project using CodeIgniter which integrate with json.
I want to display data from a database with onchange method.
I get an example script from the internet and I implemented it in the project that I created.
Here is the code:
controller :
Code:
function select_load($no) {
if (!empty($no)) {
$thn=$this->session->userdata('c_thn');
$bln=$this->session->userdata('c_bln');
$per="$thn$bln";
$cek=$this->m_customer->cek_cust($per,$no);
$query=$this->m_customer->load_cust($per,$no);
if ($cek->num_rows < 0) {
// (?) the code to show error message if field"last_meter"=null(no value)//
}else{
//the code to display value,if field"last_meter" is exist
$data[] = $query->row_array();
echo json_encode($data) ;
}
}
}
javascript (ist.js):
Code:
function getData(type) {
var kategori = document.getElementById("kat_" + type).value;
$.ajax({
url: site_url + "/ameter/" + type + "_load/" + kategori,
dataType:"json",
success:function(data){
$.each(data, function(i,n){
document.getElementById("name_" + type).value = n["name"] ;
document.getElementById("last_meter_" + type).value = n["last_meter"] ;
});
},
error: function(data){
document.getElementById("name_" + type).value = "" ;
document.getElementById("last_meter_" + type).value = "" ;
}
});
}
view:
Code:
<scrip src="<?php echo base_url();?>public/js/ist.js" type="text/javascript"></scrip>
<scrip type="text/javascript">
var site_url = "<?php echo site_url() ;?>" ;
</scrip>
<tr><td>ID Customer</td><td>:</td>
<td><? echo form_dropdown('no', $kode, NULL,'id="kat_select" onchange="getData(\'select\'); return false;"'); ?></td></tr>
<tr><td>Customer</td><td>:</td>
<td><input type="text" name="nm" readonly="readonly" id="name_select"></td></tr>
<tr><td>Last meter</td><td>:</td>
<td><input type="text" name="meter1" readonly="readonly" id="last_meter_select"></td></tr>
my question is:
-how to rewrite the script controller and javasscript(ist.js)to display alert box if field"last_meter"=null(no value)
-when the value of field"last_meter" doesn't exist, input box"nm" show "undefined",how to change this message using alert box
I hope somebody can help me to find the solution.
Thanks before, for your kindness ^_^
Regards,
Lix