使用php中的ajax在dropdownlist中获得结果

时间:2018-02-19 20:05:30

标签: php ajax

我希望从db下拉列表中获取学期结果,同时从上一个下拉列表中选择课程。我是php的初学者。以下是我试过的代码。

AJAX代码

<script>
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").value=xmlhttp.responseText;

            }
        };
        xmlhttp.open("GET","getuser1.php?q="+str,true);
        xmlhttp.send();
    }
}
</script>

HTML

<tr class="light">
          <th height="43">COURSE</th>
            <td><select name="course" onChange="showUser(this.value)">
  <option value="">Select a course</option>
  <option value="UG">UG</option>
  <option value="PG">PG</option>

  </select></td>

          </tr>
          <tr class="dark">
            <th height="43">SEMESTER</th>
            <td>

            <select name="sem" id="txtHint">

            </select>

              </td>

GETUSER1.php

<?php
$q =$_GET['q'];
mysql_connect("localhost","root","");
mysql_select_db("internal");

$sql="SELECT * FROM tblcourse WHERE course = '".$q."'";
$result = mysql_query($sql);


while($row = mysql_fetch_array($result)) {

    echo  "<option>".$row['sem']."</option>";


}

?>

0 个答案:

没有答案