簡體   English   中英

html和php中下拉列表的動態填充

[英]Dynamic population of drop down list in html and php

我正在研究學生出勤系統項目,我正在使用HTML和PHP我需要解決這個問題

我有4個下拉列表在第first drop down list包含Civil Engg,Comp Engg等.....等等 second dropdown list主題名稱存儲在mssql數據庫中,並且根據在第一個下拉列表中選擇的值,我需要從數據庫填充主題,如

select * from allsubjects where branch='civilengg';

所以請任何給我這個場景的解決方案

是一個關於動態依賴選擇框的教程,使用Ajax和jQuery可能對你有幫助

另一種解決方案是:

在您的<select name="X">代碼中添加一個id,例如<select name="X" id ="X">

把另一個選擇如<select name="Y" id="Y"> 哪個是空白的。

把這個jquery放在你的頁面中。

$("X").on("change",function(){
    var x_value=$("X").val();
    $.ajax({
        url:'ajax.php',
        data:{subject:x_value},
        type: 'post',
        success : function(resp){
            $("#Y").html(resp);               
        },
        error : function(resp){}
    });
});

在你的ajax.php中添加查詢。

<?php
$row = mysqli_query("SELECT * from allsubjects WHERE branch =".$_POST['branch']);
while($row2 = mysqli_fetch_array($row))
    echo '<option value="' . $row2['subId'] . '">' . $row2['subName'] . '</option>
?>

暫無
暫無

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

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