Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3800114
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:50:33+00:00 2026-05-19T13:50:33+00:00

I have a CodeIgniter MVC application. I have a model called city that has

  • 0

I have a CodeIgniter MVC application.

I have a model called city that has a method:

<?php
class Cities_model extends Model{    

function Cities_model(){
    parent::Model();
}

function get_cities($id = null){
    $this->db->select('id, city_name');

    if($id != NULL){
        $this->db->where('country_id', $id);
    }

    $query = $this->db->get('cities');

    $cities = array();

    if($query->result()){
        foreach ($query->result() as $city) {
            $cities[$city->id] = $city->city_name;
        }
        return $cities;
    }else{
        return FALSE;
    }  
}
...

Controller:

function Post(){
    $this->load->helper('form');
    $this->load->model('cities_model');
    $this->load->model('country_model');
    $this->load->model('field_model');

    $data['cities'] = $this->cities_model->get_cities();//optional $id parameter 
    $data['countries'] = $this->country_model->get_countries();

    $data['fields'] = $this->field_model->get_fields(); //subject field

    $this->load->view('post_view',$data);
}

This method is used to fill the contents of a dropdown list. I have a similar model for countries, which also has a dropdown.

You can see that the get_cities method is set up to accept a country_id. This would be used to filter the results if a country was selected.

My question is I need help calling this method using Ajax (preferably jQuery). I’m new to ajax and have never done anything like this before.

Any help most appreciated!

Billy

UPDATE

I’ve added jquery library and have created method in my controller:

function get_cities($country){
    header('Content-Type: application/x-json; charset=utf-8');
    echo(json_encode(array($this->cities_model->get_cities($country))));
}

here is my javascript on my view:

<script type="text/javascript">

  $(document).ready(function(){

  $('#country').change(function(){
    var country_id = $('#country').val();  // here we are taking country id of the selected one.
    $.ajax({
     type: "POST",
     url: "<?php echo base_url(); ?>home/get_cities/"+country_id,
     data: ({country : country_id}),
     success: function(cities){
       $.each(cities,function(i,city)
       {
          var opt = $('<option />');
          opt.val(city.value);
          opt.text(city.text);
          $('#cities').append(opt);
       });
    }

  });

  });
  });

This is the json reply:

[{"2":"Accra"}]

So it is successfully retriving the correct data. the only problem is it’s adding blank values/text to the drop down. and each time I change the city the data is being added on, so I guess i’d have to clear the dropdown first?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T13:50:34+00:00Added an answer on May 19, 2026 at 1:50 pm

    extending Alpesh’s answer:

    you should have a controller’s function which return cities filtered by country:

    /controller/get_cities/<country>
    

    i’m assuming that your controller’s function will return a json object, you can obtain it by doing:

    function get_cities($country)
    {
       header('Content-Type: application/x-json; charset=utf-8');
       echo(json_encode(array($this->cities_model->get_cities($country))));
    }
    

    then on the view you’ll have 2 select boxes:

    1. one filled up with the contries
    2. one empty and to be filled up with the cities retrived via ajax

    now, you have to write something like Alpesh did in order to retrive the cities of the selected country; URL will be

    url: '/controller/get_cities/'+country_id
    

    while the success function would be something like

    success: function(cities)
    {
       $('#cities').empty();
       $.each(cities,function(i,city)
       {
          var opt = $('<option />');
          opt.val(city.value);
          opt.text(city.text);
          $('#cities').append(opt);
       });
    }
    

    UPDATE
    in your ajax success function you are dealing with a json objects, infact you are doing this:

    opt.val(city.value);
    opt.text(city.text);
    

    where city is a json object and value and text are its properties.

    when you generate the json via php you have to respect what you use in jquery so your model should return an array like this:

    array
    (
       array("value"=>"2","text"=>"Accra"),
       array("value"=>"3","text"=>"Kumasi"),
       ....
    );
    

    the json_encode that array and it should work. Maybe you don’t need to wrap the model call into an array but i’m not sure depens on how you return the array

    echo(json_encode(array($this->cities_model->get_cities($country))));
    

    or

    echo(json_encode($this->cities_model->get_cities($country)));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a php application that doesn't use the mvc model that i would
I have a PHP web application built with CodeIgniter MVC framework. I wish to
I am using codeigniter php MVC framework to develop a web application. I have
I have used PHP for awhile now and have used it well with CodeIgniter,
I've run across an interesting PHP/SOAP error that has me stymied. After searching I
I come from a Codeigniter MVC background that we've been working with for some
How do I configure a CodeIgniter app so I can have one directory that
I have a problem with CodeIgniter .htaccess file and hope that somebody can help
I'm using the Codeigniter framework and have tried to stick with the MVC philosophy
In Kohana/CodeIgniter, I can have a URL in this form: http://www.name.tld/controller_name/method_name/parameter_1/parameter_2/parameter_3 ... And then

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.