简体   繁体   English

使用第二选择标签的值填充第三选择标签,第二选择标签的值使用第一选择标签从数据库中获取

[英]Populate the third select tag using the value of second select tag which has its values from the database using the first select tag

I used Ajax to populate the values of second select tag but I am not able to populate the third select tag which should appear after the selection of the second select tag. 我使用Ajax填充了第二个select标签的值,但是我无法填充应该在选择第二个select标签之后出现的第三个select标签。 Please suggest what's wrong. 请提出问题。

index.php


<html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <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 getclass(clas) {       

            var strURL="findbooks.php?clas="+clas;
            var req = getXMLHTTP();

            if (req) {

                req.onreadystatechange = function() {
                    if (req.readyState == 4) {
                        // only if "OK"
                        if (req.status == 200) {                        
                            document.getElementById('booksdiv').innerHTML=req.responseText;                     
                        } else {
                            alert("There was a problem while using XMLHTTP:\n" + req.statusText);
                        }
                    }               
                }           
                req.open("GET", strURL, true);
                req.send();
            }       
        }
        function getbooks(clas,nme) {       
            var strURL="findprice.php?clas="+clas+"&nme="+nme;
            var req = getXMLHTTP();

            if (req) {

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

        }
    </script>
    </head>
    <body>
    <form method="post" action="" name="form1">
    <table width="60%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="150">Class</td>
        <td  width="150"><select name="class" onChange="getclass(this.value)">
        <option>select class</option>
        <?php
     for ($i=1;$i<=10;$i++)
    {
        ?>
                <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
                <?php } ?>
            </select></td>
      </tr>
      <tr style="">
        <td>Books</td>
        <td ><div id="booksdiv"><select name="Books" >
        <option>Select books</option>
            </select></div></td>
      </tr>
      <tr style="">
        <td>City</td>
        <td ><div id="pricediv"><select name="price">
        <option>Select price</option>
            </select></div></td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
      <tr>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
      </tr>
    </table>
    </form>
    </body>
    </html>

findbooks.php findbooks.php

<?php 
    $class=$_GET['clas'];
    include "localhost.php";
    $query="SELECT * FROM books WHERE class='".$class."'";
    $result=mysql_query($query);

    ?>
    <select name="books" onchange="getbooks(<?php $class; ?>,this.value)" >
    <option>Select books</option>
    <?php 
    while($row=mysql_fetch_array($result))
     { 
     ?>
    <option><?php echo $row['name'];?></option>
    <?php 
    } 
    ?>
    </select>

findprice.php




<?php $class=$_GET['clas'];
    $name=$_GET['nme'];
    include "localhost.php";
    $query="SELECT price FROM books WHERE class='$class' AND name='$name'";
    $result=mysql_query($query);

    ?>
    <select name="price">
    <option>Select Price</option>
    <?php 
    while($row=mysql_fetch_array($result)) 
    { 
    ?>
    <option><?php $row['price'] ?></option>
    <?php } ?>
    </select>

The solution is to use Jquery LIVEQUERY . 解决方案是使用Jquery LIVEQUERY Livequery utilizes the power of jQuery selectors by binding events or firing callbacks for matched elements automatically, even after the page has been loaded! Livequery通过绑定事件或自动触发匹配元素的回调来利用jQuery选择器的功能,即使页面已加载!

 $(document).ready(function() {

//Populate the third 'select' tag 'onchange' of second 'select' tag.
    $('secondSelectTagID').livequery('change', function(){
           //Add necessary code to populate third dropdown.
    });
});

Note: You have to include both jquery.js and jquery.livequery.js for this approach to work. 注意:您必须同时包含jquery.jsjquery.livequery.js ,此方法才能起作用。

In findprice.php findprice.php

<?php 
$class=$_GET['clas'];
 include "localhost.php";
 $query="SELECT * FROM books WHERE class='".$class."'";
 $result=mysql_query($query);
?>
<select name="books" onchange="getbooks(<?php echo $class; ?>,this.value)" ><!--Notice the echo here-->
<option>Select books</option>
<?php 
 while($row=mysql_fetch_array($result))
 { 
?>
<option value="<?php echo $row['name'];?>"><?php echo $row['name'];?></option><!--Notice the option value here-->
<?php 
 } 
?>
</select>

And in findprice.php 并在findprice.php

<?php $class=$_GET['clas'];
$name=$_GET['nme'];
include "localhost.php";
$query="SELECT price FROM books WHERE class='$class' AND name='$name'";
$result=mysql_query($query);

?>
<select name="price" onchange="getbooks(<?php echo $class; ?>,<?php echo $name; ?>,this.value)>
<option>Select Price</option>
<?php 
while($row=mysql_fetch_array($result)) 
{ 
?>
<option><?php echo $row['price'] ?></option><!--Notice the echo here-->
<?php } ?>
</select>

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

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