简体   繁体   中英

populate select tag in php

I have a many masters in my project of the basic one is country,state and city. i have added/inserted the records in mysql database but when i want to edit record how can i populate the state select tag when the country is selected from mysql database?

For example i have a record in city table as

╔════╦══════════════╦════
║ COUNTRY║  STATE║ CITY ║
╠========╬══════════════╬
║  INDIA ║ PUNJAB║ ABC  ║
║  INDIA ║ J&K   ║ PQR  ║
║  USA   ║ NJ    ║ MNO  ║
=========================

NOW when i go for edit operation for say suppose record 1 ie INDIA-PUNJAB-ABC

I am able to select INDIA in Country select tag but i am not able to get only INDIA states and select ABC in select tag of state

However you achieve this, you will need to interrogate the db using the selected COUNTRY. So..

  1. Option 1 :perform an AJAX request 'onChange' of the COUNTRY to populate the CITY box using Javascript

  2. Option 2 : refresh/submit the page 'onChange'of the COUNTRY and use the posted COUNTRY field to generate a filtered list of CITY options to use in the

You need to normalize your data. You need tables country and state. And in your table use city_id and state_id or only state_id.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM