php - 使用mysql数据库在php中动态构建下拉列表

标签 php mysql ajax

这就是我一直在尝试做的事情:

  1. 我有一个下拉列表,用于选择学期。
  2. 选择学期后,在下一个下拉列表中,该学期的相关类(class)应显示为选项。
  3. 每次我换学期时都会发生这种情况。

数据库结构如下:

  • 数据库 - scifac
  • 表-course_info
  • 列 - courseID:varchar(主键)、courseName varchar、学期 int。

我尝试通过教程使用 ajax 来执行此操作,但它不起作用。

这是我的 html 页面

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>


<script language="javascript" type="text/javascript">
<!-- 
//Browser Support Code
function ajaxFunction()
{
 var ajaxRequest;  // The variable that makes Ajax possible!
 try
 {
   // Opera 8.0+, Firefox, Safari
   ajaxRequest = new XMLHttpRequest();
 }
 catch (e)
 {
   // Internet Explorer Browsers
   try
   {
      ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
   }
   catch (e) 
   {
      try
      {
         ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e)
      {
         // Something went wrong
         alert("Your browser broke!");
         return false;
      }
   }
 }
 // Create a function that will receive data 
 // sent from the server and will update
 // div section in the same page.
 ajaxRequest.onreadystatechange = function()
 {
   if(ajaxRequest.readyState == 4)
   {
      var ajaxDisplay = document.getElementById('ajaxDiv');
      ajaxDisplay.innerHTML = ajaxRequest.responseText;
   }

 }
 // Now get the value from user and pass it to
 // server script.
 //--------------------------------------------
 /*var age = document.getElementById('age').value;
 var wpm = document.getElementById('wpm').value;
 var sex = document.getElementById('sex').value;
 var queryString = "?age=" + age ;
 queryString +=  "&wpm=" + wpm + "&sex=" + sex;
 ajaxRequest.open("GET", "ajax-example.php" + queryString, true);
 ajaxRequest.send(null); */
 var sem=document.getElementById('year').value;
 var queryString="?sem="+sem;
 ajaxRequest.open("GET","ajax-example.php" + queryString, true);
 ajaxRequest.send(null);
}
//-->
</script>

<form name='frmform'>
        <select id='year' onchange='ajaxFunction()'>
            <option value="1">Year I Sem I</option>
            <option value="2">Year I Sem II</option>
        </select>
                <!--<input type='button' onclick='ajaxFunction()' value='Query MySQL'/>-->
        <select id="ajaxdiv">
        </select>
</form>
<!--<div id='ajaxDiv'>Your result will display here</div>-->
</body>
</html>

我的php页面(ajax-example.php)如下

$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "scifac";

//Connect to MySQL Server
mysql_connect($dbhost, $dbuser, $dbpass);
//Select Database
mysql_select_db($dbname) or die(mysql_error());
// Retrieve data from Query String
$sem = $_GET['year'];
$sem=(int)$sem;

$query = "SELECT courseID FROM course_info WHERE semester = '$sem'";



//Execute query
$qry_result = mysql_query($query) or die(mysql_error());


// Insert a new row in the table for each person returned
while($row = mysql_fetch_array($qry_result)){


    $display_string.="<select>";
    $display_string.="<option>$row[courseID]</option>"; 
}
echo "Query: " . $query . "<br />";
$display_string .= "</select>";
echo $display_string;
?>

非常感谢您在这方面给予的善意考虑。

最佳答案

您可以在 ajax-example.php 中尝试此操作:

基本上,您需要更改 $_GET['sem'] 来代替 $_GET['year']

 $dbhost = "localhost";
    $dbuser = "root";
    $dbpass = "";
    $dbname = "scifac";

    //Connect to MySQL Server
    mysql_connect($dbhost, $dbuser, $dbpass);
    //Select Database
    mysql_select_db($dbname) or die(mysql_error());
    // Retrieve data from Query String
    $sem = $_GET['sem'];
    //$sem=(int)$sem;

    $query = "SELECT courseID FROM course_info WHERE semester = '".$sem."'";



    //Execute query
    $qry_result = mysql_query($query) or die(mysql_error());

    $display_string.="<select>";

    // Insert a new row in the table for each person returned
    while($row = mysql_fetch_array($qry_result)){



        $display_string.="<option value='".$row[courseID]."'>".$row[courseID]."</option>"; 
    }

    $display_string .= "</select>";

    echo $display_string;
    ?>

关于php - 使用mysql数据库在php中动态构建下拉列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24730464/

相关文章:

php - 在函数中两次使用 Config::set() 的方法

php - 从数组中获取最大的偶数

php - 将行查询输出到 php 中的表

php - 提交时更新数据库字段

php - mySQL 将 8000 多行从一个表传输到另一个 PHP

javascript - Ajax成功函数无法在自定义函数内访问

PHP array_shift() 对我不起作用。我究竟做错了什么?

PHP 7.1 fatal error : Allowed memory size of 268435456 bytes exhausted

javascript - Processing.js 线程

javascript - jquery ajax 出错