Welcome Guest, Not a member yet? Register   Sign In
problem dependent select with GetJson
#1

[eluser]amina[/eluser]
I have dependent select with country and cities. The problem is that the getjson does not work. It does not bring the information of the BD.
My controller:
Code:
class contact extends CI_Controller{

function __construct() {
  parent::__construct();
}

public function index() {
  $data['main_content']= 'contact';
  $data['title']= 'Conexion';
  $this->load->view('includes/template',$data);
                  
}
public function verCity(){
                
  $this->load->model('Model_cbo','mc');
  echo $data['dt_local']=$this->mc->getCity($this->input->post('id'));
}
}
My model:
Code:
?php
class Model_cbo extends CI_Model{
    
    function Modelo_cbo(){
      parent::__construct();  
    }
    
  
    function getCountry(){
return $this->db->query("SELECT * FROM  countries order by nombre ASC");
    }
  
    function getCity($idcoun){
        
        $query="SELECT * FROM  cities where id_countries = '".$idcoun."'" ;
        $data=$this->db->query($query, $idcoun);
        
        echo json_encode($data->num_rows());
        
      
    }

}
My view:
Code:
<label for="country"class="label">Country</span></label>
<select name="f_country" id="f_country" class="input">
<option value='-1'></option>
<option value="&lt;?= $data-&gt;id ?&gt;">&lt;?= $data->name ?&gt;</option>
</select>
<label for="city" class="label">City<span class="req">*</span></label>
<select name="f_city" id="f_city" class="input"></select>
my js:
Code:
$(function(){
    $("#f_country").change(function(){
       if($(this).val()!=""){
            $('#f_city').show();
            
            $.ajax({
                    type:"POST",
                    url: URL_BASE+'contact/verCity',
                    data: {
                    dato: $(this).val()
               },
          success:function(){

              $.getJSON("&lt;?php echo base_url()?&gt;contact/verCity",dato, function(json){

                  $('#f_city').html('');
                  debugger;

                  $.each(json.result, function(i,item){

                  $('#f_city').append('<option value="'+i+'">'+item+'</option>');
                            });
                        });

                    }
            });
        }
        else{
            $('#f_city').empty().attr("disabled","disabled");
        }
    });
});


Someone can help me. Thanks very much
#2

[eluser]Jason Stanley[/eluser]
try adding the json header to your controller.

Code:
header('Content-type: application/json');
#3

[eluser]amina[/eluser]
I try but nothing
Add it in:
Code:
public function verCity(){
  header('Content-type: application/json');              
  $this->load->model('Model_cbo','mc');
  echo $data['dt_local']=$this->mc->getCity($this->input->post('id'));
}

#4

[eluser]Mat-Moo[/eluser]
Your ajax post sends dato but you function looks for id?
#5

[eluser]amina[/eluser]
This is what I do not if this well
[code]
success:function(){
//I call the method json for get
$.getJSON("&lt;?php echo base_url()?&gt;contact/verCity",dato, function(json){
//I erase the content of the option select
$('#f_city').html('');
debugger;
//I cross the rows of the result of json
$.each(json.result, function(i,item){
//I introduce the option of the result json
$('#f_city').append('<option value="'+i+'">'+item+'</option>');
});
});

}
});
#6

[eluser]CroNiX[/eluser]
Is your js located within a php file or a standalone js file? I ask because in your js you have
Code:
url: URL_BASE+'contact/verCity',
and
Code:
$.getJSON("&lt;?php echo base_url()?&gt;contact/verCity",dato, function(json){
If its in a js file it won't parse that php without explicitly setting your server up to parse js files as php files.

Also, your verCity function isn't sending json. Its sending a regular php array. And if you echo a php array, you just get "array".

If you were using firebug you could be examining the responses to the ajax calls and see these errors very easily.
#7

[eluser]amina[/eluser]
it's located in a standalone js file. In the firebug I have this
Code:
$is not defined
contact.js()
$(function(){
#8

[eluser]CroNiX[/eluser]
Well, you can't have php in a standalone js file. So you should change that and use URL_BASE like you did earlier instead.

And your verCity should be something like:
Code:
public function verCity(){              
  $this->load->model('Model_cbo','mc');
  $data['dt_local']=$this->mc->getCity($this->input->post('dato'));//you aren't sending "id", you're sending "dato"
  header('Content-type: application/json');
  echo json_encode($data);  //need to send this as json, not php array
}

#9

[eluser]amina[/eluser]
Thanks for your response but when I include function js?
#10

[eluser]jmadsen[/eluser]
are you certain your model function is even working correctly?

Code:
function getCity($idcoun){
        
        $query="SELECT * FROM  cities where id_countries = '".$idcoun."'" ;
        $data=$this->db->query($query, $idcoun);
        
        echo json_encode($data->num_rows());
        
      
    }

should be

Code:
function getCity($idcoun){        
        $query="SELECT * FROM  cities where id_countries = ?" ;
        $data=$this->db->query($query, array($idcoun));        
        echo json_encode($data->num_rows());      
      
    }




Theme © iAndrew 2016 - Forum software by © MyBB