Ajax数据库下拉值问题

时间:2018-07-05 06:39:29

标签: php html ajax

我需要使用Ajax从表中获取值以立即获取

在第一个下拉菜单中,如果我选择一个值,则将使用where条件在第二个下拉菜单中显示该值。

当所选值匹配时,其产品行值将显示在下拉列表中。

但是在我的情况下,它在第二个下拉框中同时显示了名称列值和产品行值。

我只希望在下拉框中显示产品行。

Ajax-Javasscript

<script type="text/javascript">
function showUser(str) {
    if (str == "") {
        document.getElementById("txtHint").innerHTML = "";
        return;
    } else { 
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                document.getElementById("txtHint").innerHTML = this.responseText;
            }
        };
        xmlhttp.open("GET","productionrecordnew.php?q="+str,true);
        xmlhttp.send();
    }
}
</script>

HTML-PHP

     <div class="control-group">
        <label class="control-label">Machine Name</label>
        <div class="controls">
        <?php
              $q = strval($_GET['q']);
              $con = mysqli_connect("mysql.hostinger.in","u403197521_ukadm","1fe[YTwS8A^jL^i","u403197521_prs");
              $query = mysqli_query($con,"SELECT * FROM machinedetails");
        ?>             
              <select name="mach_name" onchange="showUser(this.value)"> 
              <?php while ($line = mysqli_fetch_array($query, MYSQL_ASSOC)) { ?> <option value="<?php echo $line['mach_name'];?>"> <?php echo $line['mach_name'];?> </option>   
          <?php } ?>
              </select>
        </div>
     </div>
     <div class="control-group">
        <label class="control-label">Component Name</label>
        <div class="controls">
           <?php 
            $query = mysqli_query($con,"SELECT * FROM machinedetails WHERE mach_name='".$q."'");
            ?>
            <select name="product" id="txtHint"> 
            <?php while ($line = mysqli_fetch_array($query, MYSQL_ASSOC)) { ?> <option value="<?php echo $line['product'];?>"> <?php echo $line['product'];?> </option>   
            <?php } ?>
            </select>
        </div>
     </div>

0 个答案:

没有答案