CodeIgniter Forums

Full Version: How to auto retrieve data from mysql database in select box using another select box?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a following MySQL Table named customers:-

[Image: main-qimg-00d3730e3b64960e7414cfa69d1540..._webp=true]

In the above table -> company_name: there are duplicate records, that means same company having different contact person.

Following is my question:

I want two select box in codeigniter application:
1st Select box should retrieve unique values from company_name field of above table.
2nd Select box should retrieve the name of contact_person where company_name is selected in the 1st select box.

(For example: If ABC Company is selected in 1st Select box, then 2nd Select box should show only John Doe and Jane white)

How can I do this in codeigniter? It would be a great help if anyone can provide me codeigniter code for above.

Thanks in advance.
First thing first if you have duplicate records then your table is not properly normalize.
Try redesigning your table with "Data Modelling".
The thing you describe is actually not simple to do properly.

You should have an AJAX call sending the first select box choice back to the server to repopulate the second select box. Some people do the dirty way, of having ALL the data sent to the page, and use js to populate the second select box as a subset of the total data. This is ok for small sets I suppose but is not generally a good idea.

Because of all the differences in the way your site is set up, which js package you may be using, possible routes in CI, etc., it is not really possible to give you any code that would be relevant. The actual db lookups are relatively easy.

Generally, I try to avoid populating one select box with the choice of a previous one, simply because there are often easier ways to design the UI.

Good luck and best wishes,