从属下拉列表 [英] Dependent drop down list
问题描述
我试图制作一个简单的依赖下拉列表,这是表格
| | id | | | pid | | | 类别 | | |
| | 1 | | | 0 | | | 动物 | | |
| | 2 | | | 0 | | | 水果 | | |
| | 3 | | | 1 | | | 兔子 | | |
| | 4 | | | 2 | | | 柠檬 | | |
下拉列表级别1的php代码:
I tried to make a simple dependent drop down list and here is the table
| | id | | | pid | | | category | | |
| | 1 | | | 0 | | | animal | | |
| | 2 | | | 0 | | | fruit | | |
| | 3 | | | 1 | | | rabbit | | |
| | 4 | | | 2 | | | lemon | | |
php code for drop down list level 1:
<select name="level1" class="level1" onChange="get_level2(this.value)">
<option selected="selected">--level 1--</option>
<?php
$sql_level1 = "SELECT * FROM table WHERE pid=0";
$result_level1 = mysql_query($sql_level1);
while($row_level1 = mysql_fetch_array($result_level1))
{
echo "<option value='".$row_level1['id']."'>".$row_level1['kategori']."</option>";
}
?>
</select>
1级下拉菜单可以正常工作,但是我不知道如何解析pid级别中的选定ID,以根据pid值使2级下拉菜单..
如何在javascript中将1级onChange设为1级选择的id成为2级pid?
仍在搜寻,但仍无法解决我的问题...
如果我尝试将ID解析为这样的另一个文件,这是正确的吗?
函数get_level2(id)
{
$ .ajax({
类型:"POST",
网址:"../include/level2.php",/*水果或动物的ID将发送到此文件*/
beforeSend:function(){
$(#level2").html(< option> Loading ...</option>");
},
数据:"level2_id =" + id,
成功:功能(msg){
$(#level2").html(msg);
}
});
}
这是level2.php文件:
The level 1 drop down work fine, but I down know how to parsing the chosen id in level 1 to make drop down level 2 based on the pid value..
How to make the level 1 selected id become level 2 pid when level 1 onChange in javascript??
Still googling but still don''t solved my problem...
if i try to parsing the id to another file like this, is it correct??
function get_level2(id)
{
$.ajax({
type: "POST",
url: "../include/level2.php", /* The fruit or animal id will be sent to this file */
beforeSend: function () {
$("#level2").html("<option>Loading ...</option>");
},
data: "level2_id="+id,
success: function(msg){
$("#level2").html(msg);
}
});
}
and this is the level2.php file:
<?php
include "../include/sqlConnect.php";
$level2_id = $_REQUEST['level2_id'];
$sql_level2 = "SELECT * FROM table WHERE pid = '".$level2_id."'";
$result_level2 = mysql_query($sql_level2);
echo "<select name='level2'>";
while($row_level2 = mysql_fetch_array($result_level2))
{
echo "<option value='".$row_level2['id']."'>".$row_level2['kategori']."</option>";
}
echo "</select>";
?>
谁能帮助我..
Can anyone help me please..
推荐答案
sql_level1 SELECT * FROM table WHERE pid = 0" span>
sql_level1 = "SELECT * FROM table WHERE pid=0";
result_level1 > mysql_query(
result_level1 = mysql_query(
sql_level1); span> while(
sql_level1); while(
这篇关于从属下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!