Feed select options from DB depending on another select options

后端 未结 1 754
灰色年华
灰色年华 2021-01-16 23:20

I have a form that has two select fields, one representing the region and one the name of the city/village/etc. I have a database with all these entries in the following for

1条回答
  •  情话喂你
    2021-01-16 23:42

    Just use ajax for this, when one select change fetch data from the server to feed other select.

    
    
    
    

    and you script.php should return JSON from db:

    if (isset($_GET['region'])) {
        $sql = new mysqli('localhost','username','password','database');
        $region = mysqli_real_escape_string($sql,$_GET['region']);
        $query = "SELECT * FROM cities WHERE region = $region";
        $ret = $sql->query($query);
        $result = array();
        while ($row = $ret->fetch_assoc()) {
             $result[] = array(
                 'value' => $row['id'],
                 'name' => $row['city']
             );
        }
        echo json_encode($result);
    }
    

    0 讨论(0)
提交回复
热议问题