php - How to return dropdown menu to the default option based upon another menu? -


i built country , city drop down menus ajax in php. work fine. problem want if select country , after select option "please select" city option return "please select"

how modify ajax code ?

<script type="text/javascript">     // <![cdata[     $(document).ready(function(){            $('#country').change(function(){ //any select change on dropdown id country trigger code                     $("#cities > option").remove(); //first of clear select items             var country_id = $('#country').val();  // here taking country id of selected one.             $.ajax({                 type: "post",                 url: "get_cities/"+country_id, //here calling our user controller , get_cities method country_id                  success: function(cities) //we're calling response json array 'cities'                 {                     $.each(cities,function(id,city) //here we're doing foeach loop round each city id key , city value                     {                         var opt = $('<option />'); // here we're creating new select option each city                         opt.val(id);                         opt.text(city);                         $('#cities').append(opt); //here append these new select options dropdown id 'cities'                     });                 }              });          });     });     // ]]> </script> 

// dropdown select menu

<label for="country">country: </label> <select id="country" name="country_id">     <option selected="selected" value="#">please select</option>     <?php foreach ($countries $country) { ?>         <option value="<?= $country['id'] ?>"><?= $country['country_name'] ?></option>      <?php } ?> </select>  <label for="city">city: </label> <select id="cities" name="city_id">     <option selected="selected" value="#">please select</option>     <?php foreach ($cities $city) { ?>         <option value="<?= $city['id'] ?>"><?= $city['city_name'] ?></option>     <?php } ?> </select> 

simply don't proceed ajax , clearing city options if selected option please select.

$('#country').change(function(){     var country_id = $(this).val();    if(country_id === '#') return; // return if selected country option default.       // , rest of code     $("#cities > option").remove();  });   

Comments

Popular posts from this blog

Change php variable from jquery value using ajax (same page) -

Pull out data related to my apps from Android Play Store and iOS App Store -

How can I fetch data from a web server in an android application? -