Üzerinde çalıştığım projemde ajax il ilçe seçimi yaptırmaya çalışıyorum.
İller geliyor ancak ilçeleri getiremiyorum.
Yardımcı olabilir misiniz?
İllerin olduğu tablo: cities
CityID
CityName
İlçelerin olduğu tablo: town
TownID
TownName
CityID
PHP kodlarım:
?> <script src="https://code.jquery.com/jquery-3.1.1.min.js" type="text/javascript"></script> <?php $citiesList = $db->query("select * from cities")->fetchAll(PDO::FETCH_ASSOC); ?> <form action="" method="POST" onsubmit="return false;" id="newaddressform"> <div class="customer-login text-left"> <h4 class="title-1 title-border text-uppercase mb-30">YENİ ADRES EKLE</h4> <input type="text" placeholder="Adres başlık" name="title"> <select type="text" name="sehir" id="city"> <?php foreach ($citiesList as $key => $value){ echo '<option value="'.$value['CityID'].'">'.$value['CityName'].'</option>'; } ?> </select> <select id="town" name="ilce"></select> <script type="text/javascript"> $(document).ready(function(){ $("#ilce").hide(); $("#sehir").change(function(){ var CityId = $(this).val(); $.ajax({ type:"POST", url:"inc/ajax.php", data:{"city":CityID}, success:function(e) { $("#ilce").show(); $("#ilce").html(e); } }); }) }); </script> <input type="text" placeholder="Mahalle, Cadde, sokak, bina no, daire no" name="content"> <button type="submit" onclick="newaddress();" id="newaddres" class="button-one submit-button mt-15">ADRES EKLE</button> </div> </form>ajax kodlarım
<?php $CityId = $_POST['CityId']; $townList = $db->query("SELECT * FROM town WHERE city='".$CityID."'")->fetchAll(PDO::FETCH_ASSOC); foreach ($townList as $key => $value){ echo '<option value="'.$value['TownID'].'">'.$value['TownName'].'</option>'; } ?>