HTML选择onchange更新查询

时间:2012-04-19 15:17:55

标签: php javascript mysql html

我有一个包含两个选择下拉列表的表单。一个与国家,一个与大学。国家/地区下拉列表来自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" ;

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

2 个答案:

答案 0 :(得分: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" ;
?>

答案 1 :(得分: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

     }
 });