根据以前的下拉值填充第三个菜单

时间:2014-04-29 12:50:58

标签: php jquery mysql ajax

我有三个下拉菜单,前两个工作正常,第三个给我同样的头部疼痛。 由于某种原因,一旦第二个下拉值改变,它就会丢失第一个菜单的值。这是代码:

   <script language="javascript" type="text/javascript">
       function getXMLHTTP() { //function to return the xml http object
            var xmlhttp=false;  
            try{xmlhttp=new XMLHttpRequest();
            }
            catch(e){try{   xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
                }catch(e){
                    try{
                    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
                    }
                    catch(e1){
                        xmlhttp=false;
                    }}}
                return xmlhttp;
        }
        function getColor(CategoryId) {     

            var strURL="getColor.php?Category="+CategoryId;
            var req = getXMLHTTP();

            if (req) {

                req.onreadystatechange = function() {
                    if (req.readyState == 4) {
                        // only if "OK"
                        if (req.status == 200) {                        
                            document.getElementById('qcolor').innerHTML=req.responseText;                       
                        } else {
                            alert("There was a problem while using XMLHTTP:\n" + req.statusText);
                        }
                    }               
                }           
                req.open("GET", strURL, true);
                req.send(null);
            }       
        }

直到这里,所有这些似乎都有效,其余部分是错误的,但不确定是什么:

function getBrand(CategoryId,ColorId) {     

    var strURL="getBrand.php?Category="+CategoryId+"&Color="+ColorId;
    var req = getXMLHTTP();

    if (req) {

        req.onreadystatechange = function() {
            if (req.readyState == 4) {
                // only if "OK"
                if (req.status == 200) {                        
                    document.getElementById('qbrand').innerHTML=req.responseText;                       
                } else {
                    alert("There was a problem while using XMLHTTP:\n" + req.statusText);
                }
            }               
        }           
        req.open("GET", strURL, true);
        req.send(null);
    }

}

HTML代码:

 </head>
    <body>
    <div id="Quick_find_2">
             <div id="Quick_find_container">
               <form action="search2.php" method="get">
                 <div id="qcategory_1">Product</div>
                 <div id="qcategory">
                   <select name="Category" class="dropmenu" id="Category" onChange="getColor(this.value)">
                     <option value="">Any</option>
                     <option value="Keyboard"<?php if ($_GET['Category']=="Keyboard") {echo "selected='selected'"; } ?>>Keyboard</option>
                     <option value="Piano"<?php if ($_GET['Category']=="Piano") {echo "selected='selected'"; } ?>>Piano</option>
                   </select>
                 </div>

                 <div id="qcolor_1">Colour</div>
                 <div id="qcolor"><select name="Color" id="Color" class="dropmenu">
        <option value="">Select Color</option>
            </select>

                 </div>
                 <div id="qbrand_1">Brand</div>
                 <div id="qbrand"><select name="Manufacturer" class="dropmenu">
                     <option value="">Any</option> </select>

                 </div>

getColor.php

<? $Category= $_GET['Category'];
mysql_select_db($database_dconn, $dconn);
$query="SELECT DISTINCT Color FROM products WHERE products.Category LIKE '%$Category%'  AND Category!= 'Stage Pianos' AND Category!= 'Recent Pianos' AND Category!= 'Recent Keyboards' AND hidden ='no' ORDER BY Color";
$result=mysql_query($query);

?>
<select name="Color" onChange="getBrand(this.value)">
 <?php  
              echo '<option value="">Any</option>';
             while ($Color = mysql_fetch_array($result)) {
    $selected2 = $_GET['Color'] == $Color['Color'] ? 'selected' : '';
    echo '<option '.$selected2.'>' . $Color['Color'] . '</option>';
} ?>
</select>

这是getBrand.php

<? $Category= $_GET['Category'];
$Color=$_GET['Color'];
mysql_select_db($database_dconn, $dconn);
$query="SELECT DISTINCT Manufacturer FROM products WHERE products.Category LIKE '%$Category%' AND Color = '$Color' AND Category!= 'Stage Pianos' AND Category!= 'Recent Pianos' AND Category!= 'Recent Keyboards' AND hidden ='no' ORDER BY Manufacturer";
$result=mysql_query($query);


?>
<select name="Manufacturer">
<option value="">Select Brand</option>
<? while($row=mysql_fetch_array($result)) { ?>
<option value=<?=$row['Manufacturer']?>><?=$row['Manufacturer']?></option>
<? } ?>
</select>

这个输出的颜色是正确的,但是一旦改变为选中它就不会回显出类别值,或者至少当我试图进入getBrand文件时它无法找到有没有办法发送这个值类别以及getBrand文件?

欢迎任何帮助

1 个答案:

答案 0 :(得分:3)

在你的getColor.php文件中:

答:您没有在选项标签中传递值属性。

B:在getBrand函数中,您获得两个变量;一个是CategoryId,另一个是ColorId,但在品牌选择框的Onchange功能中,您只传递一个值THIS.VALUE

编辑代码:

<? $Category= $_GET['Category'];
mysql_select_db($database_dconn, $dconn);
$query="SELECT DISTINCT Color FROM products WHERE products.Category LIKE '%$Category%'  AND Category!= 'Stage Pianos' AND Category!= 'Recent Pianos' AND Category!= 'Recent Keyboards' AND hidden ='no' ORDER BY Color";
$result=mysql_query($query);

?>
<select name="Color" onChange="getBrand('<?php echo $Category; ?>',this.value)">
<?php  
          echo '<option value="">Any</option>';
         while ($Color = mysql_fetch_array($result)) {
$selected2 = $_GET['Color'] == $Color['Color'] ? 'selected' : '';

echo '<option '.$selected2.' value="'.$Color['Color'].'">' . $Color['Color'] . '</option>';
} ?>
</select>