简体   繁体   English

AJAX多次下拉

[英]AJAX Multiple Drop Downs

I'm looking off of this site to make a multiple drop down: Roshan's Blog 我正在寻找该网站,以进行多次下拉: Roshan的博客

And most of it is working, I'm just having a problem with the 3rd drop down box. 而且大多数功能都可以使用,但是我对第三个下拉框有疑问。
(Dropdown1: Clients, Dropdown2:Location, Dropdown3:Zone) (下拉列表1:客户端,下拉列表2:位置,下拉列表3:区域)

On my page so far, if I look at the source, after selecting the first dropdown(Client1), the second dropdown statement says: 到目前为止,在我的页面上,如果我查看源代码,则在选择第一个下拉菜单(Client1)之后,第二个下拉菜单语句显示:
<select style="width: 150px;" id="add-event-dialog-location" name="add-event-dialog-location" onchange="getZone(Client1,this.value)">

Which is what I need, but now, when I click on one of the options in the second drop down, it is not placing through the getZone() script. 这是我需要的,但是现在,当我单击第二个下拉菜单中的一个选项时,它并没有通过getZone()脚本放置。 The 'zonediv' is not changing, and I'm not sure if the rest is going through or not. “ zonediv”没有改变,我不确定其余部分是否正在处理。 If I load the getZone.php by itself and place in my own GET statements into the URL, I get results, but I can't get them within the page I'm calling the dropdowns from. 如果我自己加载getZone.php并将其放置在自己的GET语句中到URL中,则会得到结果,但无法在调用下拉列表的页面中得到它们。

I'm probably just missing something small, but I've been looking at it so long, that I just can't figure it out. 我可能只是想念一些小东西,但是我已经看了很久了,以至于我无法弄清楚。

The HTML: 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>

Both of the JS Functions: 两种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: 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: 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>

Try putting quotes around Client1 -- without quotes, javascript thinks it's a variable, and since you haven't defined any variable called Client1, you're getting an error. 尝试在Client1周围加上引号-没有引号,javascript认为这是一个变量,并且由于您尚未定义任何名为Client1的变量,因此会出现错误。 Putting quotes around it makes it a string, which is what you want to pass to getZone(). 用引号引起来使它成为一个字符串,这就是您想要传递给getZone()的内容。

Try putting this in getLocation.php: 尝试将其放在getLocation.php中:

<select style="width: 150px;" id="add-event-dialog-location" name="add-event-dialog- location" onchange="getZone('<?=$client?>',this.value)">

If any of your Client names have quotation marks in them, you'll have to make sure to escape them, see here for how to do that: Pass a PHP string to a JavaScript variable (and escape newlines) . 如果您的任何客户端名称中都带有引号,则必须确保对其进行转义,请参见此处,了解如何进行此操作: 将PHP字符串传递给JavaScript变量(并转义换行符)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM