【发布时间】:2016-05-12 18:34:27
【问题描述】:
我正在尝试连接 2 个下拉菜单,因此在第一个下拉列表中,我显示国家列表,并根据国家/地区的选择显示所选国家/地区的城市列表。
我的 index.php 文件可以正确加载所有国家/地区,如下图所示:
加载我的国家/地区的代码
<select name="country" id="country">
<?php
$db = pg_connect("$db_host $db_name $db_username $db_password");
$query = "SELECT country FROM countries";
$result = pg_query($query);
if (!$result) {
echo "Problem with query " . $query . "<br/>";
echo pg_last_error();
exit();
}
printf ("<option value=Select>Select a Country</option>");
while($myrow = pg_fetch_assoc($result)) {
printf ("<option value=$myrow[country]>$myrow[country]</option>");
}
?>
</select>
现在我正在尝试根据前一个“选择”中的选择做同样的事情,但它不起作用。我遇到的问题是在国家选择中选择值,因为如果我硬输入一个国家的值,例如: $query = "SELECT city FROM cities where country = Albania";然后它工作。我还尝试打印所选国家/地区的值: (echo $selectedCountry;) 它没有打印任何内容,所以我猜 $selectedCountry = $_GET['country'];或 $selectedCountry = $_POST['country'];正在获取所选国家/地区的值。
<select name="city" id="city">
<?php
$db = pg_connect("$db_host $db_name $db_username $db_password");
$selectedCountry = $_GET['country'];
$selectedCountry = $_POST['country'];
echo $selectedCountry;
$query = "SELECT city FROM cities where country = ' $selectedCountry '";
$result = pg_query($query);
if (!$result) {
echo "Problem with query " . $query . "<br/>";
echo pg_last_error();
exit();
}
printf ("<option value=Select>Select a City</option>");
while($myrow = pg_fetch_assoc($result)) {
printf ("<option value=$myrow[city]>$myrow[city]</option>");
}
?>
</select>
非常感谢您
更新
这是我在第一次加载中看到的。根据上图,国家选择加载了所有值,而城市选择为空(仅“选择城市”值)等待加载取决于国家选择的值。
最新更新 - 来自 Borna 的建议
博尔纳,
我已经尝试了您的建议,低于我正在使用的确切代码。以两个国家为例。但是,城市在第一次加载时是空的,当我选择一个国家时,城市选择中没有任何加载,我得到以下屏幕。它似乎实际上没有调用/运行 getCities.php:
索引.html
<!DOCTYPE html>
<html>
<head>
<script>
function populateCities(citiesSelectBoxOptions){
document.getElementById("city").innerHTML = citiesSelectBoxOptions;
}
function httpGetAsync(theUrl, callback)
{
alert(theUrl);
var xmlHttp = new XMLHttpRequest();
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200)
callback(xmlHttp.responseText);
}
xmlHttp.open("GET", theUrl, true); // true for asynchronous
xmlHttp.send(null);
}
</script>
</head>
<body>
<select name="country" id="country" onchange="httpGetAsync('http://localhost/FillCity/getCities?country=' + this.value, populateCities)">
<option value="Angola">Angola</option>
<option value="Spain">Spain</option>
</select>
<select name="city" id="city">
</select>
</body>
</html>
getCities.php
<?php
include("config.php");
$db = pg_connect("$db_host $db_name $db_username $db_password");
$selectedCountry = $_GET['country'];
echo "country: " .$country;
$query = "SELECT city FROM cities where country = ' $selectedCountry '";
$result = pg_query($query);
if (!$result) {
echo "Problem with query " . $query . "<br/>";
echo pg_last_error();
exit();
}
printf ("<option value='Select'>Select a City</option>");
while($myrow = pg_fetch_assoc($result)) {
printf ("<option value='$myrow[city]'>$myrow[city]</option>");
}
?>
更新
如果我运行 http://localhost/FillCity/getCities?country=Spain 这就是我得到的
如果我只运行http://localhost/FillCity/getCities.php,我会得到:(我得到了 country 这个词,因为我在代码中放置并回显 $country 以查看所选国家/地区)
这是我的本地主机下的 FillCity 文件夹 (var/www/html)
这是我第一次以安哥拉作为默认国家/地区运行 Index.html 时看到的内容。
如果我选择任何国家,例如西班牙,这就是我得到的
最后更新
当我打开 .html 文件并选择一个国家/地区时,这就是我得到的(仍在屏幕上打印该消息):
单击“确定”后,它就可以工作了,我可以看到该国家/地区的所有城市(但我当然不希望弹出该消息)
再次感谢
【问题讨论】:
-
第一次加载 index.php 时,两个下拉菜单都已经加载了吗?我的意思是,您说它可以很好地填充国家/地区列表,但它是否也在同时构建城市列表,并显示所有城市,无论国家/地区,还是什么?
-
@P.Gearman 这并没有改变,因为他正在使用 $_GET['country'] 的值。我的建议是您尝试使用原生选择框,看看它是否有效。
-
@Borna 什么是原生选择框?谢谢
-
好吧,我试图了解第二个下拉列表是通过 AJAX 调用加载还是一直存在。
-
@P.Gearman 首先加载所有国家/地区均按照所附图像加载。但是,城市未加载。城市选择器只有:“选择城市”值。谢谢
标签: php postgresql drop-down-menu