Php HTML选择一次更改更新查询

Php HTML选择一次更改更新查询,php,javascript,mysql,html,Php,Javascript,Mysql,Html,我有一个带有两个选择下拉列表的表单。一个是国家,一个是大学。国家下拉列表是从显示所有输入国家的MySQL数据库填充的。首先,我希望大学下拉列表中包含系统中的所有大学,但是最终用户希望系统是动态的,并在运行过程中进行学习,因此此时下拉列表不会显示任何数据 <select name="academicdropdown" onchange="countrysortlist()" style="width:178px"> <option value"">Sort By Count

我有一个带有两个选择下拉列表的表单。一个是国家,一个是大学。国家下拉列表是从显示所有输入国家的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>

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

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

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

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

函数countrysortlist()
{
document.form_name.submit();
}
当您更改选择选项时,网站将更新提交表单

<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" ;
?>

我已经像你说的那样创造了一切。我已经包含了jquery文件,然后我将ajax代码放在我正在使用的另一个js文件的countrysortlist函数中,并且我已经完成了我的php页面以获取结果并打印查询结果。但是,当我选择国家/地区时,没有任何变化?(我已经把一些样本大学放进去进行测试)你知道什么可能是错误的吗?我所做的一切听起来都正确吗?谢谢。从那以后,我得到了国家选择权,可以做一些事情,但是大学下拉列表中没有显示任何内容。在“加载”页面上显示所有大学,但当我按国家排序时,uni下拉列表变为空白,不显示该国家的uni。你知道为什么他们没有出现吗?另外两个问题是:我是否使用echo或print,是否还需要包含我在上面代码中显示的if语句,或者在运行排序查询后这些语句是否仍然会出现?我还可以像我已经在做的那样展示所有正在加载的大学吗?或者这会影响它为什么不运行?做一件事。。只需提醒success:函数中的数据,查看php页面中的值……否则您必须更改您的php代码。当我提醒数据时,我得到的只是[object]?我的php文件如下:数据库的连接详细信息,通过执行$_POST[“id”]获取国家id,检查id是否为数字all equals all,如果为“all”,则再次执行查询以获取所有大学,然后执行while循环,并通过打印“$university\n”来打印所有数据;。这与else部分相同,但有一个WHERE子句,按国家id发送到unis。关于PHP需要更改的内容,您有什么想法吗?我如何把我所有其他的if语句放在打印部分呢?让我们一个接一个地做。首先打印国家id和注释的剩余代码,然后打印sql查询等。。。并逐步调试。
<?php
$country_id = $_GET['academicdropdown'];
$universitysql = "SELECT * FROM university where countryid = '$country_id' ORDER BY university_name ASC" ;
?>