我使用以下代码填充三重下拉列表。它正在工作,但第三次下拉也必须以第一个下拉列表中选择的值为条件。如何更改此代码以使第3个下拉列表以第一个和第二个下拉值为条件。
这是我的代码:
<?php
ini_set("display_errors", 0); //turn off error reporting!
$data=@$_GET['data'];
$val=@$_GET['val'];
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "legalwise";
mysql_pconnect($dbhost,$dbuser,$dbpass) or die ("Unable to connect to MySQL server") ;
if ($data=='categoria') {
echo "<select name='categoria' onChange=\"dochange('subcategoria', this.value)\">\n";
echo "<option value='0'>== SELECT ==</option>\n";
$result=mysql_db_query($dbname,"SELECT name FROM firm GROUP BY name");
while(list($nomeCategoria)=mysql_fetch_array($result)){
echo "<option value=\"$nomeCategoria\" >$nomeCategoria</option> \n" ;
}
} else if ($data=='subcategoria') {
echo "<select name='subcategoria' onChange=\"dochange('subcategoria2', this.value)\">\n";
echo "<option value='0'>== SELECT ==</option>\n";
$result=mysql_db_query($dbname,"SELECT hub FROM firm WHERE name='$val' GROUP BY hub");
while(list($name)=mysql_fetch_array($result)){
echo "<option value=\"$name\">$name</option> \n" ;
}
} else if ($data=='subcategoria2') {
echo "<select name='subcategoria2' >\n";
echo "<option value='0'>== SELECT ==</option>\n";
$result=mysql_db_query($dbname,"SELECT id, area FROM firm WHERE hub='$val'");
while(list($id, $name)=mysql_fetch_array($result)){
echo "<option value=\"$id\" >$name</option> \n" ;
}
}
echo "</select>\n";
?>
Here is the other part of my program that calls this one.
<?php
require_once('startsession.php');
require_once('connectvars.php');
require_once('header.php');
include('menu.php');
?>
<form action="" method="post">
</BR>
<?php
echo "<font id=\"categoria\"><select>\n";
echo "<option value='0'>Select the Firm</option> \n" ;
echo "</select></font>\n";
?>
<?php
echo "<font id=\"subcategoria\"><select>\n";
echo "<option value='0'>Select the Claims Hub</option> \n" ;
echo "</select></font>\n";
?>
<?php
echo "<font id=\"subcategoria2\"><select>\n";
echo "<option value='0'>Select the Area</option> \n" ;
echo "</select></font>\n";
?>
<!DOCTYPE html PUBLIC >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Add Mattertype</title>
</head>
<body>
<script language=Javascript>
function Inint_AJAX() {
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {} //IE
try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE
try { return new XMLHttpRequest(); } catch(e) {} //Native Javascript
alert("XMLHttpRequest not supported");
return null;
};
function dochange(src, val) {
var req = Inint_AJAX();
req.onreadystatechange = function () {
if (req.readyState==4) {
if (req.status==200) {
document.getElementById(src).innerHTML=req.responseText;
}
}
};
req.open("GET", "add_mattertype_firmb.php?data="+src+"&val="+val);
req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); // set Header
req.send(null);
}
window.onLoad=dochange('categoria', -1);
</script>
<?php
// Write out our query.
$query = "SELECT * FROM mattertype ORDER by name";
// Execute it, or return the error message if there's a problem.
$result = mysql_query($query) or die(mysql_error());
$dropdown = "<select name='mattertype'>";
while($row = mysql_fetch_assoc($result)) {
$dropdown .= "\r\n<option value='{$row['name']}'>{$row['name']}</option>";
}
$dropdown .= "\r\n</select>";
?>
<form method="post" align= "right">
<table >
<tr><td> </td>
<tr>
<td>Select the Matter Type: </td>
<td><?php echo $dropdown; ?></td>
</tr>
<tr><td> </td>
<tr>
<td style="text-align: left; ">Active </td>
<td align="left" >
<input type="checkbox" name="active" value="1" />
</td>
</tr>
<tr><td> </td>
<tr>
<td> </td>
<td><input type="submit" name="submit" value="Add" align= "right" /></td>
</tr>
</table>
<?php
print "<pre>";
print_r($_POST);
print "</pre>";
if (isset($_POST['submit']))
{
// Connect to the database
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$firm = $_POST['categoria'];
$hub = $_POST['subcategoria'];
$area = $_POST['subcategoria2'];
$mattertype = $_POST['mattertype'];
$firm=trim($_POST['firm']) ;
$active=trim($_POST['active']);
$mistakes = array();
if (empty($name) || (!(ctype_alpha($name)))) {
$mistakes[] = 'ERROR - Your title is either empty or should only contain ALPHABET CHARACTERS.';
}
else
{
// accept title and sanitize it
$name = mysql_real_escape_string(stripslashes($_POST['name']));
}
if (sizeof($mistakes) > 0) {
echo "<ul>";
foreach ($mistakes as $errors)
{
echo "<li>$errors</li>";
//echo "<a href='areatitle_index.php'>Back...</a>";
}
echo "</ul>";
echo '<br />';
}
else {
$sql = "INSERT INTO `firm_mattertype`(firm, hub, area, mattertype, active) VALUES ('$firm', '$hub', '$area', '$mattertype', '$active')";
echo $sql;
$result = mysql_query($sql);
if(!$result) {
echo "Saved.";
//echo "<BR>";
}
else{
echo "ERROR - The same Title already exist in the database";
}
}
}
?>
</form>
<table class="table2" >
<br>
<tr>
<th> Firm </th>
<th> Hub </th>
<th> Area </th>
<th> MatterType </th>
<th> Active </th>
<th colspan = '1' > </th>
</tr>
<?php
// Connect to the database
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$result=mysql_query("SELECT * FROM firm_mattertype ORDER BY firm");
while($test = mysql_fetch_array($result))
{
$id = $test['id'];
echo "<tr align='center'>";
//echo"<td><font color='black'>" .$test['id']."</font></td>";
echo"<td><font color='black'>" .$test['firm']."</font></td>";
echo"<td><font color='black'>" .$test['hub']."</font></td>";
echo"<td><font color='black'>" .$test['area']."</font></td>";
echo"<td><font color='black'>" .$test['mattertype']."</font></td>";
echo"<td><font color='black'>" .$test['active']."</font></td>";
echo"<td> <a href ='area_view.php?id=$id'>Edit</a>";
echo "</tr>";
}
// close connection
mysql_close();
?>
</table>
</body>
</html>