Php 如何通过从另一个下拉列表中选择值来填充下拉列表

Php 如何通过从另一个下拉列表中选择值来填充下拉列表,php,javascript,jquery,html,drop-down-menu,Php,Javascript,Jquery,Html,Drop Down Menu,我有两个第一个说国家 country_id country _name 1 India 2 Australia 3 Netherlands the 2nd table is states state_id country state name 1 2 abc 2

我有两个第一个说国家

country_id          country _name
1                   India
2                   Australia
3                   Netherlands

the 2nd table is states
state_id           country         state name
1                   2              abc
2                   2              xyz
3                   3              pqr
4                   1              lmn
5                   1              rst
其中,第一个表中的country_id是第二个表中的国家
现在我想要两个下拉列表,一个是国家,另一个是州。第二个下拉列表值必须根据第一个下拉列表中选定的项目进行更改,并且只要第一个下拉列表中的项目未被选中,第二个下拉列表必须被禁用

<?php $sql = "SELECT * FROM country";
$result = mysql_query($sql);
?>
<select id="country" name='country' onchange="get_states();">
<option value=''>Select</option>
<?php while ($row = mysql_fetch_array($result)) {
    echo "<option value='" . $row['country_id'] . "'>" . $row['country_name'] . "</option>";}
?>
</select>
<div id="get_state"></div> // Sub will be appended here using ajax
编写一个ajax函数获取状态

<script type="text/javascript">
function get_states() { // Call to ajax function
    var country = $('#country').val();
    var dataString = "country="+country;
    $.ajax({
        type: "POST",
        url: "getstates.php", // Name of the php files
        data: dataString,
        success: function(html)
        {
            $("#get_state").html(html);
        }
    });
}
</script>
文件getstates.php-将从下面的文件中获取sub,该文件将附加到div

if ($_POST) {
    $country = $_POST['country'];
    if ($country != '') {
       $sql1 = "SELECT * FROM state WHERE country=" . $country;
       $result1 = mysql_query($sql1);
       echo "<select name='state'>";
       echo "<option value=''>Select</option>"; 
       while ($row = mysql_fetch_array($result1)) {
          echo "<option value='" . $row['state_id'] . "'>" . $row['state_name'] . "</option>";}
       echo "</select>";
    }
    else
    {
        echo  '';
    }
}

为Make制作一个如下所示的html

<?php $sql = "SELECT * FROM country";
$result = mysql_query($sql);
?>
<select id="country" name='country' onchange="get_states();">
<option value=''>Select</option>
<?php while ($row = mysql_fetch_array($result)) {
    echo "<option value='" . $row['country_id'] . "'>" . $row['country_name'] . "</option>";}
?>
</select>
<div id="get_state"></div> // Sub will be appended here using ajax
编写一个ajax函数获取状态

<script type="text/javascript">
function get_states() { // Call to ajax function
    var country = $('#country').val();
    var dataString = "country="+country;
    $.ajax({
        type: "POST",
        url: "getstates.php", // Name of the php files
        data: dataString,
        success: function(html)
        {
            $("#get_state").html(html);
        }
    });
}
</script>
文件getstates.php-将从下面的文件中获取sub,该文件将附加到div

if ($_POST) {
    $country = $_POST['country'];
    if ($country != '') {
       $sql1 = "SELECT * FROM state WHERE country=" . $country;
       $result1 = mysql_query($sql1);
       echo "<select name='state'>";
       echo "<option value=''>Select</option>"; 
       while ($row = mysql_fetch_array($result1)) {
          echo "<option value='" . $row['state_id'] . "'>" . $row['state_name'] . "</option>";}
       echo "</select>";
    }
    else
    {
        echo  '';
    }
}
可能的重复可能的重复