My goal is to populate several drop-down fields based on user selection. The drop-down fields I have are:
- Continent
- Country
- Sport
The desired functionality is to first select a Continent, then have the Country and Sport options populate dynamically. For example:
Europe -> (All European countries should appear based on stored data).
Upon selecting Algeria, the Sport options should populate in the drop-down. The JSON data is correct, but I suspect there is an issue with the AJAX implementation! Here's the code snippet:
$(document).ready(function(){ $('#select_continents').on('change', function(){ $('#select_countries').empty(); $('#select_sport').empty(); $.ajax({ method: 'GET', url: './json.php', data: { json_continent_country : 1, continent : $('#select_continents').val(), json_country_sport : 1, country : $('#select_countries').val() } }) .done(function(data){ $.each(JSON.parse(data), function(i, val) { $('#select_countries').append('<option value="'+val.id+'">'+val.country_name+'</option>'); $('#select_sport').append('<option value="'+val.id+'">'+val.sport_name+'</option>'); }) }) .fail(function(){ alert('error'); }) }) })
This is the current output:
https://i.sstatic.net/k6Cwp.png
Any suggestions or advice on how to improve this functionality?