【发布时间】:2012-04-24 06:51:56
【问题描述】:
我正在查看此站点以进行多重下拉:Roshan's Blog
而且大部分都在工作,我只是遇到了第三个下拉框的问题。
(Dropdown1:客户端,Dropdown2:位置,Dropdown3:区域)
到目前为止,在我的页面上,如果我查看源代码,在选择第一个下拉列表(Client1)后,第二个下拉语句显示:<select style="width: 150px;" id="add-event-dialog-location" name="add-event-dialog-location" onchange="getZone(Client1,this.value)">
这是我需要的,但是现在,当我单击第二个下拉菜单中的一个选项时,它并没有通过 getZone() 脚本放置。 “zonediv”没有改变,我不确定其余的是否通过。如果我自己加载 getZone.php 并将我自己的 GET 语句放入 URL 中,我会得到结果,但我无法在我从中调用下拉列表的页面中获得它们。
我可能只是错过了一些小东西,但我已经看了这么久,以至于我无法弄清楚。
HTML:
<select style="width: 150px;" name="add-event-dialog-client_name" id="add-event-dialog-client_name" onchange="getLocation(this.value)">
<?php
echo "<option selected='selected' disabled='disabled'>-Client Name-</option>";
$result = mysql_query("SELECT DISTINCT client_name FROM spc_clients");
while($row = mysql_fetch_array($result)){
echo "<option value='".$row['client_name']."'>".$row['client_name']."</option>";
}
?>
</select>
<p id="locationdiv">
<select style="width: 150px;" name="add-event-dialog-location" id="add-event-dialog-location" disabled="disabled">
<option>Select Client First</option>
</select>
</p>
<p id="zonediv">
<select style="width: 150px;" name="add-event-dialog-zone" id="add-event-dialog-zone" disabled="disabled">
<option>Select Location First</option>
</select>
</p>
两个 JS 函数:
function getLocation(client_name) {
var strURL="display/getLocation.php?client_name="+client_name;
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById('locationdiv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
function getZone(client_name,location) {
var strURL="display/getZone.php?client_name="+client_name+"&location="+location;
var req = getXMLHTTP();
if (req) {
req.onreadystatechange = function() {
if (req.readyState == 4) {
// only if "OK"
if (req.status == 200) {
document.getElementById('zonediv').innerHTML=req.responseText;
} else {
alert("There was a problem while using XMLHTTP:\n" + req.statusText);
}
}
}
req.open("GET", strURL, true);
req.send(null);
}
}
getLocation.php:
<?php
include 'connect.php';
$client = $_GET['client_name'];
$query="SELECT location FROM spc_clients WHERE client_name='$client'";
$result=mysql_query($query) or die(mysql_error());
?>
<select style="width: 150px;" id="add-event-dialog-location" name="add-event-dialog- location" onchange="getZone(<?=$client?>,this.value)">
<option selected='selected' disabled='disabled'>-Location-</option>
<?php
while($row = mysql_fetch_array($result)){
echo "<option value='".$row['location']."'>".$row['location']."</option>";}
?>
</select>
getZone.php:
<?php
include 'connect.php';
$client = $_GET['client_name']; echo $client;
$location = $_GET['location']; echo $location;
$query="SELECT zone FROM spc_clients WHERE (client_name='$client' && location='$location')";
$result=mysql_query($query) or die(mysql_error());
?>
<select style="width: 150px;" id="add-event-dialog-zone" name="add-event-dialog-zone">
<option selected='selected' disabled='disabled'>-Zone-</option><option><?php
while($row = mysql_fetch_array($result)){
echo $row['zone'];}
?>
</option>
</select>
【问题讨论】:
-
您是否尝试过在 Firefox 或 Chrome 的开发者工具中使用 getfirebug.com 来查找任何 javascript 错误和/或查看 AJAX 调用的结果?
-
我在 Opera 的错误控制台中收到“未捕获的异常:ReferenceError:未定义的变量:Client1”。不过,不知道为什么。
-
啊,我现在看到问题了。我会发布一个答案..
标签: php javascript html ajax drop-down-menu