I need to post a variable and get result in Codeigniter using Ajax jQuery.
Although my db has content, the code is still returning me empty array. - array(0) {}. The problem is that the variable is not getting posted from the form. Here is my code:
FORM CODE - View :
<form id = "myform1" onsubmit="return false">
<select name="field_country">
<option value="0">Argentina</option>
<option value="1">Chile</option>
</select>
</form>
Javascript - inline in view :
jQuery(document).ready(function($){
$("select[name='field_country']").change(function(){
var country_name= $("select[name='field_country'] option:selected").text();
$.post("<?= base_url(); ?>index.php/catalog/getCities/", {country:country_name}, function(data){
alert(data);
})
})
})
catalog.php controller - function :
function getCities(){
$country_name = $this->input->post('country', 'Argentina');
$result = $this->catalog_model->getCitiesNames($country_name);
var_dump($result);
}
catalog_model Model function :
function getCitiesNames($country_name) {
$cities=array();
$result = $this->db->query("SELECT title FROM cities WHERE country = '".$country_name."'");
foreach ($result->result() as $row) {
array_push($cities, $row->title);
}
return $cities;
}