簡體   English   中英

HTML選擇onchange更新查詢

[英]HTML select onchange update query

我有一個帶有兩個選擇下拉菜單的表單。 一個與國家,一個與大學。 從顯示所有輸入國家的MySQL數據庫填充國家下拉列表。 首先,我希望大學下拉列表中包含系統中的所有大學,但是最終用戶希望系統是動態的,並且可以隨着學習的進行而不斷學習,因此此時下拉列表中不會顯示任何數據。

<select name="academicdropdown" onchange="countrysortlist()" style="width:178px">
<option value"">Sort By Country</option>
<option value"All" style="font-weight:bold; font-style:italic">All Countries</option>
<?php

while ($row=mysqli_fetch_array($countryresult2)) {

    $id = $row["country_id"];
    $country = $row["country"];
    echo "<option value='$id'>$country</option>\n";
}
?>
</select>

<select name="universitydropdown" onclick="unilist()" style="width:154px">
<option value"">University</option>
<?php

    if(isset($_SESSION['appformuniversity'])) {

    if($_SESSION['appformacademic'] == "universitylist") {

        $universityinput = $_SESSION['appformuniversitylist'];
                                                    while ($row=mysqli_fetch_array($universityresult)) {
                                                        $universityid = $row["university_id"];
                                                                $university = $row["university_name"];
                                                        if($universityid == $universityinput) {
                                                            echo "<option value='$universityid' selected='selected'>$university</option>\n";
                                                        }
        else {
                                                        echo "<option value='$universityid'>$university</option>\n";
                                                        }
    }
    }
else {
                                                while ($row=mysqli_fetch_array($universityresult)) {

        $universityid = $row["university_id"];
        $university = $row["university_name"];
        echo "<option value='$universityid'>$university</option>\n";
                                                    }
}
}
else {

   while ($row=mysqli_fetch_array($universityresult)) {

            $universityid = $row["university_id"];
    $university = $row["university_name"];
            echo "<option value='$universityid'>$university</option>\n";
   }
}
?>
</select></td></tr>

我想做的是,當用戶選擇國家/地區下拉列表中的國家/地區時,它將使用該國家/地區內的大學更新大學下拉列表。 我知道您必須使用onchange事件,但是我不知道該如何執行腳本方面? 任何幫助,將不勝感激。

我目前的大學查詢是

$universitysql = "SELECT * FROM university ORDER BY university_name ASC" ;

在此先感謝您的幫助。 非常感激。

您可以使用javascript執行以下操作:

<script type="text/javascript">
function countrysortlist()
{
   document.form_name.submit();
}
</script>

當您更改選擇選項時,站點將更新提交表單

<form name='form_name' action='' method='get'>
<select name="academicdropdown" onchange="countrysortlist()" style="width:178px">
...
</select>
</form>

然后查詢將僅加載提交的國家/地區ID內的大學。

<?php
$country_id = $_GET['academicdropdown'];
$universitysql = "SELECT * FROM university where countryid = '$country_id' ORDER BY university_name ASC" ;
?>
You have to use ajax for this. If you know Jquery it would be easier. Just include the
jquery.js file . Then write the ajax in the onchange function of the country dropdown.
In the onchange method just pass the id of the country. Then in your ajax method send 
id to a php page. In that page generate the whole option list from the sql query using
that country id . Make a relation between two tables i.e every university row should 
contain country id. Then echo the option list. In your ajax method just change the 
university list dynamically using innerhtml. I am giving you example 

1. onchange on country select

 onchange="countrysortlist(this.value)"

2. ajax method in countrysortlist function

  $.ajax({
     type: 'POST',
     url: 'yourpage.php',  // change name
     data: "countryid="+id,  // country id
     success: function(data) {  // returns date
           $('.result').html(data); // result should be the class name of university dropdown

     }
 }); 

暫無
暫無

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

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