【问题标题】:How to populate a drop down based on previous drop down selection SQL如何根据先前的下拉选择 SQL 填充下拉列表
【发布时间】:2013-07-08 10:06:23
【问题描述】:

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

谢谢

<?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>

【问题讨论】:

  • 我可能遗漏了一些愚蠢的东西,但现在我可以理解了。谢谢

标签: php sql ajax drop-down-menu


【解决方案1】:
**<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>

【讨论】:

  • 我想我需要把下面的代码放到***部分吗?但我无法让它工作....... $result = mysql_query("SELECT * FROM contents WHERE parent = 0"); while(($data = mysql_fetch_array($result)) !== false) echo '
  • 我是否必须在
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-11-04
  • 2015-01-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-05
相关资源
最近更新 更多