php级联下拉框
时间: 2023-08-15 20:06:41 浏览: 98
以下是一个基于 PHP 技术实现的级联下拉框的示例代码,其中包含三个下拉框。
HTML 代码:
```html
<select id="category1" onchange="getCategory2()">
<option value="">请选择分类1</option>
<option value="1">分类1-1</option>
<option value="2">分类1-2</option>
<option value="3">分类1-3</option>
</select>
<select id="category2" onchange="getCategory3()">
<option value="">请选择分类2</option>
</select>
<select id="category3">
<option value="">请选择分类3</option>
</select>
```
JavaScript 代码:
```javascript
function getCategory2() {
var category1 = document.getElementById("category1").value;
if (category1 != '') {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
var category2 = document.getElementById("category2");
category2.innerHTML = "<option value=''>请选择分类2</option>" + xhr.responseText;
document.getElementById("category3").innerHTML = "<option value=''>请选择分类3</option>";
}
};
xhr.open("GET", "getCategory2.php?category1=" + category1, true);
xhr.send();
} else {
document.getElementById("category2").innerHTML = "<option value=''>请选择分类2</option>";
document.getElementById("category3").innerHTML = "<option value=''>请选择分类3</option>";
}
}
function getCategory3() {
var category2 = document.getElementById("category2").value;
if (category2 != '') {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200) {
var category3 = document.getElementById("category3");
category3.innerHTML = "<option value=''>请选择分类3</option>" + xhr.responseText;
}
};
xhr.open("GET", "getCategory3.php?category2=" + category2, true);
xhr.send();
} else {
document.getElementById("category3").innerHTML = "<option value=''>请选择分类3</option>";
}
}
```
PHP 代码(getCategory2.php):
```php
<?php
$category1 = $_GET["category1"];
if ($category1 == "1") {
echo "<option value='1-1'>分类1-1-1</option><option value='1-2'>分类1-1-2</option>";
} else if ($category1 == "2") {
echo "<option value='2-1'>分类1-2-1</option><option value='2-2'>分类1-2-2</option>";
} else if ($category1 == "3") {
echo "<option value='3-1'>分类1-3-1</option><option value='3-2'>分类1-3-2</option>";
}
?>
```
PHP 代码(getCategory3.php):
```php
<?php
$category2 = $_GET["category2"];
if ($category2 == "1-1") {
echo "<option value='1-1-1'>分类1-1-1-1</option><option value='1-1-2'>分类1-1-1-2</option>";
} else if ($category2 == "1-2") {
echo "<option value='1-2-1'>分类1-1-2-1</option><option value='1-2-2'>分类1-1-2-2</option>";
} else if ($category2 == "2-1") {
echo "<option value='2-1-1'>分类1-2-1-1</option><option value='2-1-2'>分类1-2-1-2</option>";
} else if ($category2 == "2-2") {
echo "<option value='2-2-1'>分类1-2-2-1</option><option value='2-2-2'>分类1-2-2-2</option>";
} else if ($category2 == "3-1") {
echo "<option value='3-1-1'>分类1-3-1-1</option><option value='3-1-2'>分类1-3-1-2</option>";
} else if ($category2 == "3-2") {
echo "<option value='3-2-1'>分类1-3-2-1</option><option value='3-2-2'>分类1-3-2-2</option>";
}
?>
```
在这个示例中,我们使用了三个下拉框,其中第一个下拉框的选项会影响第二个下拉框的选项,第二个下拉框的选项则会影响第三个下拉框的选项。在选择第一个下拉框的选项时,会通过 PHP 脚本获取第二个下拉框的选项数据,并更新第二个下拉框的选项。在选择第二个下拉框的选项时,会通过 PHP 脚本获取第三个下拉框的选项数据,并更新第三个下拉框的选项。希望这个示例对你有所帮助。
阅读全文