简体   繁体   English

HTML选择onchange更新查询

[英]HTML select onchange update query

I have a form with two select dropdowns. 我有一个带有两个选择下拉菜单的表单。 One with countries and one with universities. 一个与国家,一个与大学。 The country dropdown is populated from a MySQL database showing all countries inputted. 从显示所有输入国家的MySQL数据库填充国家下拉列表。 At first I want the university dropdown to be populated with all the universities in the system however the end user wants the system to be dynamic and learn as it goes on so at this moment in time the dropdown doesn't show any data. 首先,我希望大学下拉列表中包含系统中的所有大学,但是最终用户希望系统是动态的,并且可以随着学习的进行而不断学习,因此此时下拉列表中不会显示任何数据。

<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>

What I would like to do is when the user selects one of the countries in the country dropdown, it will update the universities dropdown with universities that are within that country. 我想做的是,当用户选择国家/地区下拉列表中的国家/地区时,它将使用该国家/地区内的大学更新大学下拉列表。 I know you have to use the onchange event but I do not know how to do the scripting side? 我知道您必须使用onchange事件,但是我不知道该如何执行脚本方面? Any help would be appreciated. 任何帮助,将不胜感激。

My university query at the moment is 我目前的大学查询是

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

Thanks in advance for any help. 在此先感谢您的帮助。 Much appreciated. 非常感激。

You can do something like this with javascript: 您可以使用javascript执行以下操作:

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

When you change the select option, the site will update submitting the form 当您更改选择选项时,站点将更新提交表单

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

Then the query will load just the universities within the country id submitted. 然后查询将仅加载提交的国家/地区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