Q:

Codeigniter - Posting form variables and retrieving through jQuery AJAX

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;
}

A:

You need to make a small modification to your Javascript and Controller code. In order for Javascript and PHP to communicate between each other, you have to tell both pieces of code what format you are using to transfer the information. The most easy and common format is JSON. Therefore you need to modify your code to send and receive JSON.
FORM VIEW
<form id="myform1" onsubmit="return false">
    <select id='country' name="field_country">
       <option value="0">Argentina</option>
       <option value="1">Chile</option>
    </select>
</form>

I have added an id='country' to your select for easier reach.
JAVASCRIPT
$(document).ready(function(){
   // When the SELECT is changed
   $("#myform1 #country").change(function(){

    var country_name= $(this).find("option:selected").text();
    $.post ( "<?= base_url(); ?>index.php/catalog/getCities/",
             { country:country_name },
             function(data){
                ...on success do something here
             },
             'json'
    );

});

});
You need to tell Javascript that you are expecting a JSON object back.
CONTROLLER
function getCities(){
   $country_name = $this->input->post('country');
   $result = $this->catalog_model->getCitiesNames($country_name);

   echo json_encode($result);
}

Your PHP code, should then return a JSON object.

