如何使用Mysql-Php和query实现链式选择
如何在此代码中添加第三个下拉列表。我一直没能弄明白。谢谢如何使用Mysql-Php和query实现链式选择,php,mysql,select,cascade,chained,Php,Mysql,Select,Cascade,Chained,如何在此代码中添加第三个下拉列表。我一直没能弄明白。谢谢 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <script type="text/javascript" src="jqueryui/js/jquery-ui-1.8.15.custom.min
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="jqueryui/js/jquery-ui-1.8.15.custom.min.js"></script>
<script type="text/javascript" src="jqueryui/js/jquery-1.6.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("select#category").change(function(){
var id = $("select#category option:selected").attr('value');
$.post("select_type.php", {id:id}, function(data){
$("select#type").html(data);
});
});
});
</script>
</head>
<body>
<?php
include("select.class.php");
?>
<form id="select_form">
Choose a category:<br />
<select id="category">
<?php
echo $opt->ShowCategory();
?>
</select>
<br /><br />
choose a type:<br />
<select id="type">
<option value="0">choose...</option>
</select>
<br /><br />
choose third drop down: <br />
<select id="third_table">
<option value="0">choose...</option>
</select>
<br /><br />
<input type="submit" value="confirm" />
</form>
<div id="result"></div>
</body>
</html>
*************************************************************************************************
$(文档).ready(函数(){
$(“选择#类别”).change(函数(){
var id=$(“选择#类别选项:已选择”).attr('value');
$.post(“select_type.php”,{id:id},函数(数据){
$(“选择#类型”).html(数据);
});
});
});
选择一个类别:
选择一种类型:
选择。。。
选择第三个下拉列表:
选择。。。
*************************************************************************************************
这就是类,我已经添加了查询第三个数据库表的代码
<?php
class SelectList
{
protected $conn;
public function __construct()
{
$this->DbConnect();
}
protected function DbConnect()
{
include "db_config.php";
$this->conn = mysql_connect($host,$user,$password) OR die("Unable to connect to the database");
mysql_select_db($db,$this->conn) OR die("can not select the database $db");
return TRUE;
}
public function ShowCategory()
{
$sql = "SELECT * FROM category";
$res = mysql_query($sql,$this->conn);
$category = '<option value="0">choose...</option>';
while($row = mysql_fetch_array($res))
{
$category .= '<option value="' . $row['id_cat'] . '">' . $row['name'] . '</option>';
}
return $category;
}
public function ShowType()
{
$sql = "SELECT * FROM type WHERE id_cat=$_POST[id]";
$res = mysql_query($sql,$this->conn);
$type = '<option value="0">choose...</option>';
while($row = mysql_fetch_array($res))
{
$type .= '<option value="' . $row['id_type'] . '">' . $row['name'] . '</option>';
}
return $type;
}
public function ShowThird()
{
$sql = "SELECT * FROM thirdtable WHERE id_type=$_POST[id2]";
$res = mysql_query($sql,$this->conn);
$third = '<option value="0">choose...</option>';
while($row = mysql_fetch_array($res))
{
$third .= '<option value="' . $row['id_third'] . '">' . $row['name'] . '</option>';
}
}
}
$opt = new SelectList();
?>
*************************************************************************************************
select_type.php
<?php
include "select.class.php";
echo $opt->ShowType();
?>
*************************************************************************************************
选择_type.php
您的jquery已更改为:
<script type="text/javascript">
$(document).ready(function(){
$("select#category").change(function(){
var id = $("select#category option:selected").attr('value');
$.post("select_type.php?event=type", {id:id}, function(data){
$("select#type").html(data);
});
});
$("select#type").change(function(){
var id = $("select#type option:selected").attr('value');
$.post("select_type.php?event=third_table", {id:id}, function(data){
$("select#third_table").html(data);
});
});
});
</script>
......
<?php
include "select.class.php";
switch ($_GET['event']) {
case 'third_table':
echo $opt->ShowThird();
break;
default:
echo $opt->ShowType();
break;
}
?>
$(文档).ready(函数(){
$(“选择#类别”).change(函数(){
var id=$(“选择#类别选项:已选择”).attr('value');
$.post(“select_type.php?event=type”,{id:id},函数(数据){
$(“选择#类型”).html(数据);
});
});
$(“选择#类型”).change(函数(){
变量id=$(“选择#类型选项:选定”).attr('value');
$.post(“select_type.php?event=third_table”,{id:id},函数(数据){
$(“选择第三个表格”).html(数据);
});
});
});
并在文件末尾选择_type.php更改为:
<script type="text/javascript">
$(document).ready(function(){
$("select#category").change(function(){
var id = $("select#category option:selected").attr('value');
$.post("select_type.php?event=type", {id:id}, function(data){
$("select#type").html(data);
});
});
$("select#type").change(function(){
var id = $("select#type option:selected").attr('value');
$.post("select_type.php?event=third_table", {id:id}, function(data){
$("select#third_table").html(data);
});
});
});
</script>
......
<?php
include "select.class.php";
switch ($_GET['event']) {
case 'third_table':
echo $opt->ShowThird();
break;
default:
echo $opt->ShowType();
break;
}
?>
。。。。。。
希望它能帮助您HTML和PHP
<h4>Search By Regions</h4>
<select id="country" name="country">
<option value="">--</option>
<?
$result1 = mysql_query("SELECT * FROM country ORDER BY name");
while($row1 = mysql_fetch_array($result1)) {
echo '<option value="'.$row1['name'].'">'.$row1['name'].'</option>';
}
?>
</select>
<select id="oblast" name="oblast">
<option value="">--</option>
<?
$result2 = mysql_query("SELECT oblast.name, country.name AS myCountry FROM oblast,country WHERE oblast.country_id = country.id ORDER BY oblast.name");
while($row2 = mysql_fetch_array($result2)) {
echo '<option value="'.$row2[0].'" class="'.$row2['myCountry'].'">'.$row2[0].'</option>';
}
?>
</select>
<select id="gorod" name="gorod">
<option value="">--</option>
<?
$result3 = mysql_query("SELECT gorod.name, oblast.name AS myOblast FROM gorod,oblast WHERE gorod.region_id = oblast.id ORDER BY gorod.name");
while($row3 = mysql_fetch_array($result3)) {
echo '<option value="'.$row3[0].'" class="'.$row3['myOblast'].'">'.$row3[0].'</option>';
}
?>
</select>
按区域搜索
--
--
--
别忘了包括这个
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="/template/js/jquery.chained.min.js"></script>
<script>
$(document).ready(function() {
$("#oblast").chained("#country");
$("#gorod").chained("#oblast");
});
</script>
$(文档).ready(函数(){
美元(“#州”)。连锁(“国家”);
美元(“#戈罗德”)。连锁(“#州”);
});