0

I want to provice and country_id together in view via json_encode but it's show the message [object] [object]

public function get_province()
{
    $country_id=$this->input->post('country_id');
    $provinces=$this->ajax->get_province($country_id);

    if(count($provinces)>0)
    {
        $pro_select_box='';
        $pro_select_box .='<option value="">-Select Province-</option>';
        foreach($provinces as $province)
        {
            $pro_select_box .='<option value="'.$province->province_id.'">'.$province->province_name.'</option>';
        }
        $cid=$province->province_id;
        $data=array(
            'pro'=>$pro_select_box,
            'cid'=>$cid
        );

        echo json_encode($data);
    }
}

Here is the javascript:

$.ajax({
    url:"get_province",
    type:"POST",
    data:{'country_id':country_id},
    dataType:'json', success:function(data){
        $('#province').html(data);
        $('#cid').val(data);
    },
    error:function(){ alert('Errror'); }
});
Conor Mancone
  • 1,940
  • 16
  • 22
Majeed
  • 149
  • 1
  • 3
  • 16

1 Answers1

0

You are getting back JSON data and trying to output it to the page as HTML. You have to build some appropriate HTML in javascript using your JSON data from the server, and then write that to the page. Something like:

success:function(data){
    $('#province').html(data.pro);
    $('#cid').val(data.cid);
}

Also worth a mention: your current setup builds the HTML in PHP and then passes it back to javascript to add to the page. You're probably better off simply passing the data down (i.e. the list of provinces) and then letting javascript build the HTML and add it to the page.

Also, you have no code to handle the possibility that there won't be any provinces found. That may be an issue for you in the long run.

Conor Mancone
  • 1,940
  • 16
  • 22