douling1936 2015-04-04 10:55
浏览 63
已采纳

如何根据两个下拉选择从数据库中检索数据

I've got the data retrieval working for one drop down selection using ajax but i want it to retrieve data based on two drop-down selection conditions. How do i parse two variables to my showChoice function and then store the 2nd choice in another variable. I would appreciate any help.

<html>
  <head>
    <script>
      function showChoice(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 (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
            }
          }
          xmlhttp.open("GET","getuser.php?q="+str,true);
          xmlhttp.send();
        }
      }
    </script>

  </head>
  <body>

    <form>
      <select name="choices" onchange="showChoice(this.value)">
        <option value="">Select a departure point:</option>
        <option value="London">London</option>
        <option value="New Castle">New Castle</option>
      </select>
    </form>
  </body>
</html>

Then the getuser.php doc

<!DOCTYPE html>
<html>
  <head>
    <style>
    table {
    width: 100%;
    border-collapse: collapse;
    }

    table, td, th {
    border: 1px solid black;
    padding: 5px;
    }

    th {text-align: left;}
    </style>
  </head>
  <body>

    <?php
      $q = $_GET["q"];


      $con = mysqli_connect('localhost','root','','busdb');
      if (!$con) {
        die('Could not connect: ' . mysqli_error($con));
      }

      mysqli_select_db($con,"busdb");
      $sql="SELECT * FROM busRoutes WHERE Departure = '".$q."'";
      $result = mysqli_query($con,$sql);


      echo "<table>
      <tr>
      <th>ID</th>
      <th>Departure</th>
      <th>Destination</th>
      <th>Time</th>

      </tr>";
      while($row = mysqli_fetch_array($result)) {
        echo "<tr>";
        echo "<td>" . $row['ID'] . "</td>";
        echo "<td>" . $row['Departure'] . "</td>";
        echo "<td>" . $row['Destination'] . "</td>";
        echo "<td>" . $row['Time'] . "</td>";
        echo "</tr>";
      }
      echo "</table>";


      mysqli_close($con);
    ?>
  </body>
</html>
  • 写回答

1条回答 默认 最新

  • dongluxin6711 2015-04-04 11:04
    关注

    you can use like this , have a look. may be you need to include jquery library if you missed this

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    

    update your code like this

    function showChoice(str,sel2) {
    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 (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET","getuser.php?q="+str+"&q2="+sel2,true);
        xmlhttp.send();
    }
    }
    
    $(document).ready(function()
    {
        $('.select').change(function()
        {
            var sel1=$('#select1').val();
            var sel2=$('#select2').val();
            showChoice(sel1,sel2);
    
        })
    
    });
    

    html will look like this

    </head>
    <body>
    
    <form>
    <select id="select1" class="select" name="choices">
    <option value="">Select a departure point:</option>
    <option value="London">London</option>
    <option value="New Castle">New Castle</option>
    </select>
    
    <select id="select2" class="select" name="choices2">
    <option value="">Select a departure point:</option>
    <option value="London">London</option>
    <option value="New Castle">New Castle</option>
    </select>
    </form>
    </body>
    </html>
    

    UPDATE

    here is jsfiddle link

    update2

    getuser.php

    <?
    echo $q=$_GET['q'];
    echo $q2=$_GET['q2'];
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于#c++#的问题:水果店管理系统
  • ¥30 dbLinq最新版linq sqlite
  • ¥20 对D盘进行分盘之前没有将visual studio2022卸载掉,现在该如何下载回来
  • ¥15 完成虚拟机环境配置,还有安装kettle
  • ¥15 2024年全国大学生数据分析大赛A题:直播带货与电商产品的大数据分析 问题5. 请设计一份优惠券的投放策略,需要考虑优惠券的数量、优惠券的金额、投放时间段和投放商品种类等因素。求具体的python代码
  • ¥15 有人会搭建生鲜配送自营+平台的管理系统吗
  • ¥15 用matlab写代码
  • ¥30 motoradmin系统的多对多配置
  • ¥15 求组态王串口自定义通信配置方法或代码?
  • ¥15 实验 :UML2.0 结构建模