如何使用Mysql Php和查询实现链式选择 [英] how to implement chained select using Mysql Php and query
本文介绍了如何使用Mysql Php和查询实现链式选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在此代码中添加第三个下拉列表.我还没弄清楚.谢谢!!!
How can I add a third drop down to this code. I have not been able to figure it out. Thanks!!!
<!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>
*************************************************************************************************
这是一个类,我已经添加了查询第三个数据库表的代码
This is the class, I already added the code that queries the third database table
<?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();
?>
推荐答案
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>
这篇关于如何使用Mysql Php和查询实现链式选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文