Need help in the design, in general, I made a registration on the site where there is a country / region / city (region and city only for Russia). Now the choice works, but, not fully, but I mean, if you select Russia, it shows a block of regions, then cities (everything seems to be fine, but), if the user has already specified all the data, you cannot bring them out in the profile , you need to be like picture https://dl.dropboxusercontent.com/sh/i5ku2ld65c774bo/WYYsnHt4qV/example.png

I tried to do everything in every way, pulled it out of the functions, combined the functions, and still didn’t deduce as needed.

Here are files with the base of countries / regions and cities of Russia, as well as the ajax_city.php file in which the information is processed, and main.php where I main.php everything else, jquery and css. https://www.dropbox.com/sh/i5ku2ld65c774bo/JgAqFXTnbK

In the main.php file main.php if you replace $user_country = "0"; на $user_country = "3159"; $user_country = "0"; на $user_country = "3159"; , the result will be exactly the one for which I am looking for a solution.

Thanks for the help.

    2 answers 2

    There are a lot of such decisions in the internet:

    http://webersoft.ru/svyazannye-s-pomoschyu-ajax-selecty-jquery/

    http://xandeadx.ru/blog/javascript/36

    it is only on the vskidku ...

    • I do not need ready-made solutions, I only need a direction, and there are a lot of solutions at first glance, but it’s all wrong, or it is written in too long code, or not in Jquery. - Nazaret2005
    • @ Nazaret2005 direction is - after receiving the country id, send it to php (for example, $ .post ('countries.php', regionList, "json")), so that it finds a list of regions and cities. return in the form of json, as you get the answer, show the second select (and the third, if any) and insert the received options into it. everything. - zb '
    • @eicto the problem is that in order for jquery to process the request, you need to perform POST / GET, if <select> is id = 0 and I choose something, then it sends and receives the desired result, but when the country id is initially specified , then the POST action is not sent and thus does not output the regions. That's the problem, I do not know how to even be. - Nazaret2005
    • one
      You should call a function for each select (for example, onChange) with ajax call, and force you to send the data you need to the handler. - gart67

    Use AJAX.

     var $selectCountry = $('select[name="country"]') , $selectRegion = $('select[name="region"]').hide() , $selectCity = $('select[name="city"]').hide(); $selectCountry.change(function(){ $.ajax({ type: 'post', url: '/getRegions.php', data: { countryId : $(this).val() //код страны }, success: function(regions){ //строим список регионов var options = ''; for(i = 0, region; region = regions[i]; i++){ options += '<option val="' + region.id + '">' + region.title + '</otion>'; } $selectCity.html(options).show(); } }) }); $selectRegion.change(function(){ //подгрузить города выбранного региона }); 

    Example getRegions.php

     <?php # данные для подключения к MySQL $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "myDB"; # подключаемся к базе $conn = new mysqli($servername, $username, $password, $dbname); # проверяем соединение if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } # получить все регионы нужной страны и отсортировать их в алфавитном порядке $countryId = isset($_POST['countryId']) ? $_POST['countryId'] : null; $sql = "SELECT id, title FROM regions WHERE country_id = " . $countryId . " ORDER BY title"; $result = $conn->query($sql); #собираем регионы в массив $regions = []; while($row = $result->fetch_assoc()) { $regions[] = $row; } # не забываем отключиться от базы $conn->close(); header('Content-Type: application/json; carset=utf-8'); echo json_encode($regions); 

    By analogy with this example, it will not be difficult for you to create your handler for loading cities when choosing a region