View Full Version : Populate dropdown with another dropdown, dynamically

03-05-2008, 12:12 PM
I have 2 drop down menu's and only want options in the second one, once the user has selected their options from the first one. For the second list I also want the data to be pulled from a table in my database... Each row has a column called FirstName and a column called Reference. Reference is either Blue, Red, Green, Pink. If they select Pink then I want all the FirstName's that have Pink in their Reference column to populate into the second dropdown... Anyone know of a simple way to do this? I have seen some very complex examples...

03-05-2008, 04:47 PM
Three options the way I see it--

If you don't mind using Javascript then this option will provide the smoothest ride for your user. The idea is to build the Javascript code on the fly using PHP. You build a Javascript array and populate it with the data from your table. Then as the user makes selections from the first selectbox, use Javascript to fill the second selectbox from the Javascript array you built using PHP.

If you want to use Ajax that will also be a smooth ride for your user.

The least smooth would be to submit the form every time an option is selected in the first selectbox, taking control back over to PHP to gather the data for the second selectbox and re-create the HTML page, now with the options in the second selectbox.