簡體   English   中英

填充第二 <select>根據第一個下拉<select>下拉選項

[英]Populate second <select> drop down based on the first <select> drop down option

我試圖根據第一個選定的下拉選項填充第二個下拉菜單。

到目前為止,這是我的工作:

form.php-我使用javascript調用getgeneral.php 當用戶從(第一個) 下拉菜單中進行選擇時,將顯示第二個下拉菜單

<html>
<head>
<script type="text/javascript">
function get_states() { // Call to ajax function
    var classitem = $('#classitem').val();
    var dataString = "classitem="+classitem;
    $.ajax({
        type: "POST",
        url: "getgeneral.php",
        data: dataString,
        success: function(html)
        {
            $("#get_general").html(html);
        }
    });
}
</script>
</head>
<body>

<form action="" method="POST">

<?php 

include('conn.php');

$result=mysqli_query($con,"SELECT * FROM classtable");

?>

<select id="classitem" name="classitem" onchange="get_states();">
<option value=''>Select</option>
<?php 

while ($row = mysqli_fetch_array($result)) {
    echo "<option value='" . $row['genclasscode'] . "'>" . $row['description'] . "</option>";}

?>
</select>

<div id="get_general"></div>

</form>

</body>
</html>

這是getgeneral.php ,它將在其中從第一個下拉列表中獲取數據:

<?php

include('conn.php');

if ($_POST) {
    $classitem = $_POST['classitem'];
    if ($classitem != '') {

       $result1=mysqli_query($con,"SELECT * FROM generalclass WHERE genclasscode='$classitem'");

       echo "<select name='classitem'>";
       echo "<option value=''>Select</option>"; 
       while ($row = mysqli_fetch_array($result1)) {
          echo "<option value='" . $row['specclassid'] . "'>" . $row['description'] . "</option>";}
       echo "</select>";
    }
    else
    {
        echo  '';
    }
}

?>

問題第二個下拉列表不會顯示。 運行form.php時沒有出現錯誤

很抱歉回到這個問題為時已晚。

問題是我忘記在代碼中包含jQuery

我在<head> </head>部分之間包含了這樣的jQuery

<link rel="stylesheet" type="text/css" href="jquery-ui-1.10.4.custom/css/custom-theme/jquery-ui-1.10.4.custom.min.css">
<script src="jquery-1.9.1.min.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>

感謝BryanChitowns24在上述評論。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM