dowjgrm6787 2014-07-15 12:10
浏览 215
已采纳

从数据库中填充下拉列表的选项

Please consider the following.This is what I have been trying to do. 1)I have a drop down list which can be used to select semesters. 2)Once I select the semester,courses for that semester should appear in another drop down list. 3)As the semester changes courses may also have to change.

My database

course_info(courseID varchar(15) primary key,courseName varchar(30),semester int)

This is what I did using ajax,php and mysql.

My html page

<html>
<head>
<script>
function showsemester(str) {
  if (str=="") {
    document.getElementById("txtHint").innerHTML="";
    return;
  } 
  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","showsemester.php?q="+str,true);
  xmlhttp.send();
}
</script>
</head>
<body>

<form>
<select name="users" onchange="showsemester(this.value)">
<option value="">Select a semester:</option>
<option value="1">Y i S i</option>
<option value="2">Y i S ii</option>

</select>
</form>
<br>
<div id="txtHint"><b>courses will be listed here.</b></div>
</body>

My php page

<?php
$q = intval($_GET['q']);

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


$sql="SELECT * FROM course_info WHERE semester = '".$q."'";
$result = mysqli_query($con,$sql);

echo "<table border='1'><tr><th>course ID</th><th>name</th><th>semester</th></tr>";

while($row = mysqli_fetch_array($result)) {
  echo "<tr>";
  echo "<td>". $row['courseID'] . "</td>";
  echo "<td>" . $row['courseName'] . "</td>";
  echo "<td>" . $row['semester'] . "</td>";
  echo "</tr>";
}
echo "</table>";

mysqli_close($con);
?>

Now it produces the result in a div as a table and working properly without any errors.But I would like to take the cours names as options in a drop down list and course ID as the values.

Your kind consideration given with this regard is highly appreciated. Thank you. T

  • 写回答

1条回答 默认 最新

  • douqiang5163 2014-07-15 12:18
    关注

    Your php file code file should be like this

    <?php
    $q = intval($_GET['q']);
    
    $con = mysqli_connect('localhost','root','','scifac');
    if (!$con) {
      die('Could not connect: ' . mysqli_error($con));
    }
    
    
    $sql="SELECT * FROM course_info WHERE semester = '".$q."'";
    $result = mysqli_query($con,$sql);
    ?>
    <select name = "semester">
    <?php
    while($row = mysqli_fetch_array($result)) {
    ?>
    <option value="<?php echo $row['courseID']; ?>"><?php echo $row['courseName']; ?></option>
    <?php
    }
    ?>
    </select>
    <?php
    mysqli_close($con);
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备