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

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>
查看全部
douling1936
douling1936
2015/04/04 10:55
  • php
  • html
  • ajax
  • javascript
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

1个回复