简体   繁体   中英

javascript select city from selected country value

i have db of countries from here http://www.webmasterworld.com/html/3018309.htm with 239 countries, every country have value. After i have selected another country in option, how can i update zone list with zones from new value?

$country_query = mysql_query("SELECT * FROM country WHERE status = '1' ORDER BY name ASC");
$zone_query = mysql_query("SELECT * FROM zone WHERE country_id = '" . (int)$country_id . "' AND status = '1'");

<select name="country_id">
          <option value=""><?php echo '$text_select;' ?></option>
          <?php while ($country=mysql_fetch_array($country_query)) { ?>
          <?php if ($country['country_id'] == $country_id) { ?>
          <option value="<?php echo $country['country_id']; ?>" selected="selected"><?php echo $country['name']; ?></option>
          <?php } else { ?>
          <option value="<?php echo $country['country_id']; ?>"><?php echo $country['name']; ?></option>
          <?php } ?>
          <?php } ?>
</select>

First time by default counties list will be loaded.. to load zones use this

<select name="country" id='country'>
          <option value=""><?php echo '$text_select;' ?></option>
          <?php while ($country=mysql_fetch_array($country_query)) { ?>
          <?php if ($country['country_id'] == $country_id) { ?>
          <option value="<?php echo $country['country_id']; ?>" selected="selected"><?php echo $country['name']; ?></option>
          <?php } else { ?>
          <option value="<?php echo $country['country_id']; ?>"><?php echo $country['name']; ?></option>
          <?php } ?>
          <?php } ?>
</select>

Here you need to call ajax when country changed

JQuery :

<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function(){

    $('#country').change(function(){

        $.post('ajax/getZones', 
                   {Country : $('#country').val()}, 
                   function(response){
                     //prepare the zones html code
                     //write that code to zone dropdown 
                  }, 'json');

    });

});
</script>

Here 'ajax/getZones' refers getZones is the method in ajax controller

write the getZones query in getZones (ajax) get the Country id with $_POST['Country'] in ajax controller

try implement ajax controller everything work fine..

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