使用ajax生成动态列表

时间:2015-05-17 14:08:24

标签: javascript php html ajax

我正在尝试使用ajax生成动态列表 getSectionServer.php是:

   <?php
    include('connect.php');
$con=new connection();
session_start();
echo"<option>hi</option>";
$p=$_REQUEST['p'];
//echo "<p>heloo</p>";

$query="select sectionToken,campus from section where courseID='".$p."' and instID='".$_SESSION['username']."'";
$result1 = mysqli_query($con->getcon(),$query);
if ($result1->num_rows>0){
    while($row = mysqli_fetch_array($result1)){
        echo"<option>".$row['sectionToken'].",".$row['campus']"</option>";
    }
}   
 else 
     echo "<option> you are not teaching any section </option>";

 mysqli_close($con->getcon());**/
?>

和javaScript ajax代码是

 function getSection(){
    var x=document.getElementById('course').value;
    var xmlhttp;
  if (window.XMLHttpRequest)
  {

  xmlhttp=new XMLHttpRequest();

  }
else
  {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
   xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById('section').innerHTML = xmlhttp.responseText;

            }
        }
        xmlhttp.open("GET", "getSectionServer.php?p="+x, true);
        xmlhttp.send();

}

但是当我运行它时,选择列表框保持空白为什么?

1 个答案:

答案 0 :(得分:1)

看起来像getSectionServer.php中的语法错误:

echo"<option>".$row['sectionToken'].",".$row['campus']"</option>";

应该是:

echo "<option>" . $row['sectionToken'] . "," . $row['campus'] . "</option>";

注意额外的.