简体   繁体   中英

Codeigniter JQuery load data in view dynamically after jquery .get request

I have been thinkering around and I cant get it to work. So i have a Jquery function which after input field onblur event posts new discount value in database.

JQuery:

    function saveAtlProc(t){
    pzid = $('#pzrows').attr('rel');
    atl_proc = $(t).val();
    var site_url = document.getElementById("site_url").value;
     $.post(site_url, {
            pzid : pzid,
            atl_proc : atl_proc,
            site_url : site_url
        },
        function(rawdata){
             getPZtotals();
        });
}

This works and is okey, the problem starts with getPZtotals(),

getPZtotals() JQuery:

function getPZtotals(){
pzid = $('#pzrows').attr('rel');
var site_url = document.getElementById("pvn_rates_get").value;
$.get(site_url, {
        pzid : pzid
    },
    function(rawdata){
     var pieg_nos = rawdata.pieg_nos;
        alert(pieg_nos);
});
}

Alert function shows that the variable is undefined. And i cant figure out why.

This is what is returned in .get() inspecting it with firebug

[{"ID":"123","avr":"0","avrnr":"1502193","avrdat":"2015-03-15","avrproc":"0.00","pz":"","serija":"","numurs":"","datums":"0000-00-00","parvad":"P\u0101rvad\u0101t\u0101js: klienta transports","apm_not":"","apm":"","piez":"Jauns pied\u0101v\u0101jums","pieg_id":"667","pieg_nos":"Miks Ratnieks SIA","reg_nr":"RE\u0122ISTR\u0100CIJASNR","pvn_id":"PVNSNR","pieg_jadr":"Uliha 68-1","pieg_fadr":"Kurmja pieg\u0101des32-1","pieg_iban":"LV34012389072345987","pieg_swift":"SWED8927348","pieg_banka":"SWEDBANK","valuta":"EUR","summa":"300.00","atl_proc":"2.00","atl_summa":"6.00","summa_bpvn":"294.00","pvn":"63.00","summa_arpvn":"363.00","apm_datums":"0000-00-00","datetime":"2015-03-19 13:04:22","avanss":"","avanss_txt":"","avanss_sum":"0.00","avanss_apm":"0.00","arpvn":"","komentars":"","nol":"","statuss":"Iesakts","autors":"","pvn_summa_0":"0","pvn_summa_1":"0","pvn_summa_2":"0","pvn_likme_0":"0","pvn_likme_1":"21","pvn_likme_2":"12","pvn_0":"0","pvn_1":"0","pvn_2":"0"}]

I think that this is correct and there shouldnt be any problems.

MY Controller: which returns data in .get() function

  public function get_new_discounts(){
    $this->load->model('offers_for_clients_model');
    $id = trim($_GET['pzid']);
    $data['orders_info'] =  $this->offers_for_clients_model->get_offers_info($id);
    $this->output->set_output(json_encode($data['orders_info']));
    /*echo ();*/
}

Model:

function get_offers_info($id){
    $query = $this->db->where('ID', $id)->get('pz_pied')->result();
    return $query;
}

I googled it but couldn`t find anything similar to this.

Your php model is returning an outer array wrapping the object data you are trying to access in your alert.

Try:

 var pieg_nos = rawdata[0].pieg_nos;
 alert(pieg_nos);

This worked:

var myObject = JSON.parse(rawdata);
alert(myObject[0].pieg_nos);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM