Welcome Guest, Not a member yet? Register   Sign In
mysql and json with onchange method
#1

[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="&lt;?php echo base_url();?&gt;public/js/ist.js" type="text/javascript"></scrip>
<scrip type="text/javascript">

    var site_url    = "&lt;?php echo site_url() ;?&gt;" ;

</scrip>

<tr><td>ID Customer</td><td>:</td>
<td>&lt;? echo form_dropdown('no', $kode, NULL,'id="kat_select" onchange="getData(\'select\'); return false;"'); ?&gt;</td></tr>
<tr><td>Customer</td><td>:</td>
<td>&lt;input type="text" name="nm" readonly="readonly" id="name_select"&gt;&lt;/td></tr>
<tr><td>Last meter</td><td>:</td>    
<td>&lt;input type="text" name="meter1" readonly="readonly" id="last_meter_select"&gt;&lt;/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
#2

[eluser]zawaruddin[/eluser]
r u Indonesian?

replace to

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) {
echo '[removed]alert("your message"); document.location="../index.php"[removed]'
}else....

u can replace index.php file...




Theme © iAndrew 2016 - Forum software by © MyBB