我有两个下拉菜单。首先,您选择国家,其次,您应该能够选择城市。城市应该根据所选择的国家而有所不同。国家和城市都是从数据库中加载的。
数据库中有两个表,包含以下行:
- 国家
- countryId <
- 名称/gh>
- 城市
- cityId <
- 名称/gh>
- countryId
这是索引:
<html>
<head></head>
<body>
<select>
<?php
$con=mysqli_connect("localhost","root","","database");
$result = mysqli_query($con,"SELECT * FROM countries");
while($row = mysqli_fetch_array($result)) {
echo '<option value="' .$row['countryId'] .'">' .$row['name'] .'</option>';
}
?>
</select>
<br />
<select>
<!-- Here goes select from countries -->
</select>
</body>
如何填充二次选择?
给第一个选择框命名为firstrongelect第二个选择框命名为second_select
希望这对你有帮助
<script type="text/javascript">
$(document).ready(function() {
$("#first_select").change(function() {
$.get('getcities.php?first_select=' + $(this).val(), function(data) {
$("#second_select").html(data);
});
});
});
</script>
<form method="get">
<select name="first_select" id="first_select">
<?php
$con=mysqli_connect("localhost","root","","database") or die(mysqli_error());
$result = mysqli_query($con,"SELECT * FROM countries") or die(mysqli_error());
while($row = mysqli_fetch_array($result)) {
echo '<option value="' .$row['countryId'] .'">' .$row['name'] .'</option>';
}
?>
</select>
<select name="second_select" id="second_select"></select>
</form>
创建一个名为getcities.php和
的文件在<strong> getcities.php
$first_select= $_GET['first_select'];
$query = mysql_query("select * from cities where countryid = {$first_select}");
while($row = mysql_fetch_array($query)) {
echo "<option value='$row[cityid]'>$row[name]</option>";
}
你需要使用一些条件逻辑,为他们为他们的国家所做的选择分配一个变量,例如"countryChoice",然后在你的第二个下拉列表中,使用另一个while语句列出所有具有countryChoice的城市。
像这样:
<html>
<head></head>
<body>
<form method="post" action="/*YOUR ACTION */">
<select name='countryChoice' id='countryChoice'>
<?php
$con=mysqli_connect("localhost","root","","database");
$result = mysqli_query($con,"SELECT * FROM countries");
while($row = mysqli_fetch_array($result)) {
echo '<option value="' .$row['countryId'] .'">' .$row['name'] .'</option>';
}
?>
</select>
<br />
<select name='cityChoice' id='cityChoice'>
<?php
$countryChoice = $_POST['countryChoice'];
$con=mysqli_connect("localhost","root","","database");
$result = mysqli_query($con,"SELECT * FROM cities WHERE countryId='$countryChoice'");
while($row = mysqli_fetch_array($result)) {
echo '<option value="' .$row['countryId'] .'">' .$row['name'] .'</option>';
}
?>
</select>
</form>
你可能需要稍微改变一下,我有一段时间没有使用这个了。但是,我希望这能给你正确的想法。
或者,如上所述,您可以使用Ajax。
相关文章:
- PHP-使用内爆、爆炸和array_unique从逗号分隔的两列SQL关键字中自动填充下拉菜单
- 动态填充两个下拉菜单
- php动态双下拉菜单
- PHP更新表单有动态下拉菜单和optgroup
- 如何在HTML/PHP中创建导航栏的下拉菜单
- 如何使Jquery编码的动态下拉菜单与PHP GET变量协调工作
- 如何从下拉菜单在PHP/MySQL中插入数据
- 如何使用下拉菜单显示mysql数据
- 当选择下拉菜单时,可以运行数据库更新功能
- 带有DB中值的下拉菜单
- php中的默认选择下拉菜单
- 在文档的单独位置嵌入php内容的下拉菜单的问题
- PHP;PDO;带有下拉菜单的MySQL INSERT查询
- Magento:下拉菜单
- 下拉菜单中的PHP变量
- twitter引导程序下拉菜单
- 使用下拉菜单筛选Web内容
- Jquery动态下拉菜单
- 隐藏/显示下拉菜单取决于另一个选项
- 我如何动态填充下拉菜单取决于一些其他下拉菜单