Could someone tell me how to create a dynamic dropdown menu using php.? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Could someone tell me how to create a dynamic dropdown menu using php.?

eg. user selects a state from state tag then dynamically cities must be fetched from the database as options in select tag.

4th Jul 2017, 5:53 PM
Deepanshu
Deepanshu - avatar
4 Answers
+ 5
Without Ajax: if you want to use the data from a database, you could fetch it all at the beginning, create all the possible select with php and only show the right one with js, upon user selection of the country. Definitely not an optimal solution. Another approach would require reloading the page upon user country selection, to retreive the secondary menu data and create the corresponding select. Depending on your project it may not be ideal from a UX point of view.
4th Jul 2017, 6:39 PM
seamiki
seamiki - avatar
+ 4
Check out this Ajax example https://www.w3schools.com/php/php_ajax_database.asp instead of creating a table, as in the example, populate and show the secondary select.
4th Jul 2017, 6:33 PM
seamiki
seamiki - avatar
+ 2
No. Because the client get the executed php code from the server. without reloading the page you have to use client side programming language.
4th Jul 2017, 10:07 PM
Bumpety
+ 1
there is no alternative to ajax?
4th Jul 2017, 6:34 PM
Deepanshu
Deepanshu - avatar