Home » CAKEPHP » Dynamic dropdown in cakephp 2.0 (Selectbox)
Dynamic dropdown and select box in cakephp 2.0
Dynamic dropdown and select box in cakephp 2.0

Dynamic dropdown in cakephp 2.0 (Selectbox)

Hello Friend, In this post i want to show you how to create dynamic dropdown in cakephp 2.x using ajax auto populating Select boxes, country state city selectbox in cakephp, cake selection box, Dependent dropdown in cakephp.

  • In this tutorials i want to demonstrate, How to create dynamic dropdown in cakephp. In cakephp this task become easy to develop dynamic dropdown country state city.
  • Cakephp is one of the most popular frameworks in the market to develope web application, cakephp development team provide wonderful functionality to make it possible.
  • Click here for what is cakephp.
  • Here I show to how to call ajax and load data dynamically without page load. Country state city is the best example for explaining this concept.
  • Here i use Cakephp version 2.5.6 for explaining this demo,  Also include download file to download whole demo free free free.

Database & Tables

  • First of all open mysql and create one database dropdowns this database contains three tables countries states and cities as below. state table has relation with country table and city table has relation with state table.

   Country State City tabel

  • Cakephp follow MVC(Model View Controller) pattern That’s why it’s contain three file Model, Controller, view.

More Information about cakephp click here.

  1. Controller File (app\Controller\CountriesController.php)
  • After controller file you must have to create three model file for your table as give bellow.

2) Country Model File(app\Model\Country.php)

3) State Model File (app\Model\State.php)

4) Cities Model file (app\Model\City.php)

  • After Model file you must be create view file for front view. View Folder should be contain all the view file with appropriate controller name.

5) View File (app\View\Countries\index.ctp)

  • You Must have to include jquery  file in defult.ctp. I also give Download link to download whole demo.

Demo

Dynamic_dropdown_in_cakephp_2
Dynamic_dropdown_in_cakephp_2

 

Live Demo Download

About Bhavin Shiroya

Check Also

cakephp

What is cakephp?? for cakephp developers

  The CakePHP is a free, open-source, rapid development framework for PHP. It’s a foundational …

  • babyfaceyoochun

    haw can i add this countries and states in the database

    • Bhavin

      actually i con’t understand your problem can you give more details about it.

      • babyfaceyoochun

        i have 4 table user countries state and city . the table user have 3 attributes : id name and countries_id. i would have this dropdown in the user . i would choose country for the user

        • yes you can doit just perform “select* from countries”. after this you will get all the countries list in dropdown .
          but for this you need to establish foriegnKey.
          user table and countries table Use HasOne Countries . 1 to 1 relationship. after this maybe your question solved.

Get more stuff like this
in your inbox

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.