0

我有一个带有两个选择下拉列表的表单。一个是国家,一个是大学。国家下拉列表来自 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" ;

提前感谢您的帮助。非常感激。

4

2 回答 2

1

你可以用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" ;
?>
于 2012-04-19T16:18:25.847 回答
0
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

     }
 }); 
于 2012-04-19T15:49:34.110 回答