Home>

I need your help, the code doesn't work.

There are three tables in the database:

country: id, name

region: id, country_id, name

city: id, region_id, name

I need to display three select:

first select with countries

the second select loads regions with the selected country id

the third one accordingly loads the cities with the selected region id

Here is some code that most likely doesn't work or doesn't work as expected.

<
script >
$ ('document'). ready (function () {
    $ ('# country'). Change (function () {
        var country_id= this.value;
        $ .ajax ({
            type: "POST",
            url: "select.php",
            data: {country_id: country_id},
            cache: false,
            success: function (result) {
                $ ('# region'). Html (result);
            }
        });
    });
    $ ('# region'). Change (function () {
        var region_id= this.value;
        $ .ajax ({
            type: "POST",
            url: "select.php",
            data: {region_id: region_id},
            cache: false,
            success: function (result) {
                $ ('# city'). Html (result);
            }
        });
    });
});
<
/script >
<
form action= "" method= "POST" >
 <
select id= "country" >
  <
option value= "0" >
select a country <
/option >
  <
option value= "1" >
country1 <
/option >
  <
option value= "2" >
country2 <
/option >
  <
option value= "3" >
country3 <
/option >
 <
/select >
 <
select id= "region" >
<
/select >
 <
select id= "city" >
<
/select >
<
/form >

The list of countries is loaded immediately, but if you select a country, then nothing happens

select.php

if (! empty ($ _ POST ["country_id"])) {
    $ country_id= $ _POST ['country_id'];
    $ result= $ db->
query ("SELECT * FROM regions WHERE country_id= '$ country_id'");
    if ($ result->
num_rows >
 0) {
        echo '<
option value= "" >
select a region <
/option >
';
        while ($ row= $ db->
get_row ($ result)) {
            echo '<
option value= "'. $ row [' id '].'" >
'. $ row ['name']. '<
/option >
';
        }
    } else {
        echo '<
option value= "" >
Regions not found <
/option >
';
    }
} else if (! empty ($ _ POST ["region_id"])) {
    $ region_id= $ _POST ['region_id'];
    $ result= $ db->
query ("SELECT * FROM cities WHERE region_id= '$ region_id'");
    if ($ result->
num_rows >
 0) {
        echo '<
option value= "" >
select a city <
/option >
';
        while ($ row= $ db->
get_row ($ result)) {
            echo '<
option value= "'. $ row [' id '].'" >
'. $ row ['name']. '<
/option >
';
        }
    } else {
        echo '<
option value= "" >
No cities found <
/option >
';
    }
}

I don't know much about javascript, so I will forgive help, thanks in advance!

Returns all of the homepage code, i.e. directly duplicates 0_o

Sinatra2021-11-05 16:03:11

do you have PHP script and HTML in one file?

Хацкер2021-11-05 16:03:11

I have a CMS, I wrote a php file that creates a page

Sinatra2021-11-05 16:03:11

it is necessary that the code of the request to the database is located separately from the code that sends HTML to the user. Otherwise ajax will return all html besides php response

Хацкер2021-11-05 16:03:11

the users.php file produces the first list of countries in the page, and the select.php file is separate, and executes only the code that I described

Sinatra2021-11-05 16:03:11