$('#countries').change(function () { // bind the change event to your select input
var country = $(this).val(); // retrieve the selected country
$.get('fetch_states.php', {country: country}, function (res) { // using ajax, we retrieve the list of states/provinces associated with the country
var states = res.split('\n'); // split the ajax result into spaces, assuming the php script placed LF between each state entry
var html = ''; // create a buffer to handle the html
for (var i = 0; i < states.length; i++) {
html += '<option>' + states[i] + '</option>'; // append an option to the select
}
$('#states').html(html); // set the html buffer as the inner html of the #states select
});
});