我有一个选择国家和选择城市,我想在城市选择中显示不同的值,具体取决于国家选择的选择。
像: 值= countries.selection
SELECT CITY FROM cities WHERE Country = Value
$ myrow [城市]
请参阅下面的代码(这会为数据库中的选择带来值并填充它们。
可能有更好的方法来编码我的代码,我也非常感谢任何使其更容易和更有效的建议。
此外,如果在Javascrip或jQuery中更好或更容易,我也会接受这方面的建议。
非常感谢
<html>
<body>
<select name="taskOption" id="drop_drop">
<?php
$db = pg_connect('host=localhost dbname=test user=myuser password=mypass');
$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>
<select name="taskOption" id="drop_drop">
<?php
$db = pg_connect('host=localhost dbname=test user=myuser password=mypass');
$query = "SELECT city FROM cities";
$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>
</body>
</html>
答案 0 :(得分:0)
您必须在国家/地区更改时提出请求,将国家/地区的值传递给服务器以查找其城市。
input = gets.chomp.to_i
case input
when 1
请注意 <!-- Note id change here -->
<select name="taskOption" id="countries">
<?php
$db = pg_connect('host=localhost dbname=test user=myuser password=mypass');
$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>
<select name="taskOption" id="cities">
<?php
$db = pg_connect('host=localhost dbname=test user=myuser password=mypass');
// Note query change here.
$selectedCountry = $_GET['country'];
$query = "SELECT city FROM cities where country_id = " + $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>
<script type='text/javascript'>
// Note this new script.
$(document).ready(function(){
$('#countries').on('change', function(){
window.location += '?country=' + $(this).val();
});
});
</script>
元素中的id
属性,并注意可能的SQL注入。