Я не совсем понимаю, что вы имеете в виду под одним файлом PHP, но это код, который я использую для динамического заполнения полей выбора. В основном при изменении поля выбора выберите идентификатор и выполните Ajax через jQuery, чтобы получить JSON-ответ городов и раскрывающийся список.
Я использую этот код для заполнения поля выбора городов (из одного файла PHP) в зависимости от выбранной страны:
<script type="text/javascript">// <![CDATA[
$(document).ready(function(){
$('#country').change(function(){ //any select change on the dropdown with id country trigger this code $("select[id$=cities] > option").remove(); //first of all clear select items
var country_id = $('#country').val(); // here we are taking country id of the selected one.
$.ajax({
type: "GET",
url: "home/get_cities/"+country_id, //here we are calling our user controller and get_cities method with the country_id
success: function(cities) //we're calling the response json array 'cities'
{
$.each(cities,function(id,city) //here we're doing a foeach loop round each city with id as the key and city as the value
{
var opt = $('<option />'); // here we're creating a new select option with for each city
opt.val(id);
opt.text(city);
$('#cities').append(opt); //here we will append these new select options to a dropdown with the id 'cities'
});
}
});
});
});
// ]]>
</script>
Вы можете опубликовать данные вместо этого, если хотите. В PHP вы бы сделали что-то вроде этого:
function get_cities($country){
$this->load->model('cities_model');
header('Content-Type: application/x-json; charset=utf-8');
echo(json_encode($this->cities_model->get_cities($country)));
}
Я написал сообщение об этом здесь: http://theninthnode.com/2011/01/dropdown-filtering-with-codeigniter-and-ajax/