[eluser]Unknown[/eluser]
I need a dropdownlist Depending province -> city, that I created with jquery,I wrote a controller and a view of a model running perfectly in version 1.7.2 of CodeIgniter, but does not work in version 2.0.3 I do not understand why it works .... is loaded only the province and the town is not loaded...sorry for my poor English ... and thanks
this is the controller
Code:
<?php
class Tenda extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper('form');
$this->load->model('tenda_model');
}
function index()
{
$data['provincie']=$this->tenda_model->get_provincie();
$this->load->view('tenda_view', $data);
}
function get_comuni($provincia)
{
header('Content-Type: application/x-json; charset=utf-8');
echo json_encode($this->tenda_model->get_comuni($provincia));
}
}
this is the model
Code:
<?php
class Tenda_model extends CI_Model
{
function __construct() {
parent::__construct();
}
function get_provincie()
{
$this->db->select('id_provincia, nome_provincia');
$query =$this->db->get('provincie');
$provincie = array();
if ($query->result())
{
foreach ($query->result() as $provincia)
{
$provincie[$provincia->id_provincia] = $provincia->nome_provincia;
}
return $provincie;
}
else
{
return FALSE;
}
} //chiusura get_provincie
function get_comuni($provincia=NULL)
{
$this->db->select('id_comune, nome_comune');
if ($provincia)
{
$this->db->where('id_provincia', $provincia);
}
$query = $this->db->get('comuni');
$comuni =array();
if($query->result())
{
foreach ($query->result() as $comune)
{
$comuni[$comune->id_comune] = $comune->nome_comune;
}
return $comuni;
}
else
{
return FALSE;
}
} //chiusura get_comuni
} //chiusura file
?>
this is the view
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>tenda</title>
<style>
#tenda
{
display: block;
}
</style>
[removed][removed]
[removed]
$(document).ready(function(){
$('#provincia').change( function()
{
var id_provincia = $('#provincia').val();
$.ajax(
{
type: "POST",
url:"tenda/get_comuni/"+id_provincia,
success: function(comuni)
{
$('#comuni').empty();
$.each(comuni,function(id_comune, nome_comune)
{
var opt = $('<option />');
opt.val(id_comune);
opt.text(nome_comune);
$('#comuni').append(opt);
});
}
});
});
});
[removed]
</head>
<body>
<?php $comuni['#'] = 'seleziona'; ?>
<div id="tenda">
<label for="provincia">provincia: </label><?php echo form_dropdown('id_provincia', $provincie, '#', 'id="provincia"'); ?>
<label for="comune">comune: </label><?php echo form_dropdown('id_comune', $comuni, '#', 'id="comuni"'); ?><br />
</div>
</body
</html>