Javascript 基于用户输入的MySQL查询-多个下拉列表

Javascript 基于用户输入的MySQL查询-多个下拉列表,javascript,jquery,mysql,Javascript,Jquery,Mysql,我正在创建一个下拉选择,其中的每个子选择,查询结果来自上一个。我在找到一个类似的帖子后搜索并整理了我的作品,不幸的是,我仍然无法让它发挥作用。任何帮助都会很棒。多谢各位 我使用select作为下拉列表,id=level1、level2、level3等 这是我代码的html下拉选择部分 Enter Auction Category : <input type="text" name="fcv" id="fcv" /> <br> or Select Category : &

我正在创建一个下拉选择,其中的每个子选择,查询结果来自上一个。我在找到一个类似的帖子后搜索并整理了我的作品,不幸的是,我仍然无法让它发挥作用。任何帮助都会很棒。多谢各位

我使用select作为下拉列表,id=level1、level2、level3等

这是我代码的html下拉选择部分

Enter Auction Category : <input type="text" name="fcv" id="fcv" /> <br> 
or Select Category : <br> 
<select class="sspecial" name="level1" id="level1" onchange="getDropdownOptions()"> 
<option></option>

<? 
$level1_sql="SELECT * FROM Ebay_Category WHERE Level = '1' ORDER BY Category"; 
$level1_results=mysql_query($level1_sql); 
while($level1=mysql_fetch_array($level1_results)) { 
  if ($level1['Leaf']) { 
    echo "<option value='".$level1['ebay_category_id']."'>".$level1['Category']."</option>"; 
   } else { 
  echo "<option value=''>".$level1['Category']."</option>"; 
  } 
} 
?> 

</select> 



<br><br> 
<select class="sspecial" name="level2" id="level2"> 
<option value=''></option> 
</select> 
进入拍卖类别:
或选择类别:

jQuery插件可以满足您的需要。

populate\u L2.php
只需返回一个字符串,其中包含您想要在菜单中显示的所有
元素。您的
populateDropdown
函数看起来很适合将其插入到页面中。我让populate_L2.php building$html只填充了元素-正确吗?我会在上面加上代码是的,没错。它将替换原始HTML中的空选项元素。在populate_L2.php的末尾缺少了
echo$HTML
。我刚刚注意到一个jQuery插件可能会对您有所帮助:谢谢-我没有这样做,但它确实起作用了-最终让我们找到一个完整的解决方案。
<script type="text/javascript">
$(document).ready(function() {
    $('#level1').change(getDropdownOptions);
});

function getDropdownOptions() {
    var val = $(this).val();
    // fire a POST request to populate_L2.php
    $.post('populate_L2.php', { value : val }, populateDropdown, 'html');
}

function populateDropdown(data) {
    if (data != 'error') {
            $('#level2').html(data);
}
}


</script>
mysql_connect("$servername", "$dbusername", "$dbpassword")or die("cannot connect");
mysql_select_db("$dbname")or die("cannot select DB");
$parentid=$_GET['value'];

$postSQL="select * Ebay_Category where Level='2' and ParentID=$parentid";

$postSelect=mysql_query($postSQL);
$html = "<option>--select--</option>";

while ($row = mysql_fetch_array($postSelect)) 
{
    if($row['Leaf']!=0){
    $html .= "<option value='".$row['CategoryID']."'>".$row['Category']."</option>";
    } else {
        $html .= "<option value=''>".$row['Category']."</option>";
    }

}
    <script type="text/javascript">
    function getInfo(str) 
    {
if (str=="")
  {
  document.getElementById("catTitle").innerHTML="";
  return;
  } 
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("catTitle").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","cat_title.php?q="+str,true);
xmlhttp.send();
}
</script>
    $vx=$_GET["q"];
$SQL="SELECT * FROM Ebay_Category WHERE ParentID='".$vx."' ORDER BY Category";
$result=mysql_query($SQL);

echo "<select id='lev3' onchange='getInfo2(this.value)'>";
while ($row=mysql_fetch_array($result)){
    echo "<option value='".$row['CategoryID']."'>".$row['Category']."</option>";
}
    echo "</select>";