问题
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();
?>
回答1:
Your jquery changed to:
<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>
And end of your file select_type.php changed to:
......
<?php
include "select.class.php";
switch ($_GET['event']) {
case 'third_table':
echo $opt->ShowThird();
break;
default:
echo $opt->ShowType();
break;
}
?>
Hope it's helps you
回答2:
HTML and 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>
And dont forget to include this
<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>
来源:https://stackoverflow.com/questions/24853870/how-to-implement-chained-select-using-mysql-php-and-query