如何根据以前的下拉选择SQL填充下拉列表

时间:2013-07-08 10:06:23

标签: php sql ajax drop-down-menu

我一直在关注此解决方案1,以创建一个下拉列表,根据用户之前在页面中较高的下拉列表中选择的更改。但是我不确定我在选项中需要做什么最初会在这里填充

感谢

<?php
mysql_connect('localhost');
mysql_select_db("test");
$result = mysql_query("SELECT * FROM `contents` WHERE `parent` = 0");
echo "<select name='name'>";
while(($data = mysql_fetch_array($result)) !== false)
    echo '<option value="', $data['id'],'">', $data['name'],'</option>'
?>

        <select onchange="ajaxfunction(this.value)">
        <!-- Options would have been initially populated here -->
            </select>
        <select id="sub">    
            </select>

   <script type="text/javascript"> function ajaxfunction(parent)
{
$.ajax({
    url: 'process.php?parent=' + parent;
    success: function(data) {
        $('#sub option').remove();  //// here sub is the id of second select box
        $('#sub').append(data)
    }
});
}
</script>

1 个答案:

答案 0 :(得分:0)

**<select onchange="ajaxfunction(this.value)">
**<!-- Options would have been initially populated here -->**
</select>**


<script type="text/javascript">
function ajaxfunction(parent)
{
    $.ajax({
        url: 'process.php?parent=' + parent;
        success: function(data) {
            $('#sub option').remove();  //// here sub is the id of second select box
            $('#sub').append(data)
        }
    });
}
</script>
/// script section may be any where on your page


<select id="sub">    ////second select box
</select>