我有4个文件,如下所示:
PostVent.php:
<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "utf-8" />
<link rel="stylesheet" type="text/css" href="PostLinkStyle.css" />
<style type="text/css">
legend.standout{font-weight: bold; font-size: 24pt;}
</style>
<title>Post Vent</title>
</head>
<body>
<!-- start header div -->
<div id="header">
<h3>SomeVent</h3>
</div>
<div id="wrap">
<form action="" method="post">
<div class="row">
<div class="large-8 small-centered columns">
<fieldset>
<legend id="legend">Post Vent</legend>
<div class="row">
<div class="small-12 columns">
<label for="email">Email</label>
<input type="text" id="email" size="35"></input>
</div>
</div>
<div class="row">
<div class="small-8 columns">
<?php
//Include database configuration file
include('dbConfig.php');
include('index.php');
//Get all state data
$query = $db->query("SELECT **name** FROM states");
//Count total number of rows
$rowCount = $query->num_rows;
?>
<select name="state" id="state">
<option value="">Select State</option>
<?php
if($rowCount > 0){
while($row = $query->fetch_array()){
echo '<option value="'.$row['name'].'">'.$row['id'].'</option>';
}
}else{
echo '<option value="">States not available</option>';
}
?>
</select>
<select name="county" id="county">
<option value="">Select County</option>
</select>
</div>
</div>
的index.php:
<script src="jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#state').on('change',function(){
var stateID = $(this).val();
if(stateID){
$.ajax({
type:'POST',
url:'ajaxData.php',
data:'id='+stateID,
success:function(html){
$('#county').html(html);
}
});
}else{
$('#county').html('<option value="">Select state first</option>');
}
});
$('#county').on('change',function(){
var countyID = $(this).val();
if(countyID){
$.ajax({
type:'POST',
url:'ajaxData.php',
data:'id='+stateID,
success:function(html){
$('#county').html(html);
}
});
}else{
$('#city').html('<option value="">Select state first</option>');
}
});
});
</script>
ajax.php:
<?php
//Include database configuration file
include('dbConfig.php');
if(isset($_POST["id"]) && !empty($_POST["id"])){
//Get all county data
$query = $db->query("SELECT name FROM counties...");
//Count total number of rows
$rowCount = $query->num_rows;
//Display county list
if($rowCount > 0){
echo '<option value="">Select County</option>';
while($row = $query->fetch_assoc()){
echo '<option value="'.$row['id'].'">'.$row['name'].'</option>';
}
}else{
echo '<option value="">County not available</option>';
}
}
?>
加上我的数据库配置文件(我认为不需要显示)。我试图根据选定的状态动态填充县下拉列表。
我是非常新的,并且通过在线提供的许多教程将我所拥有的内容放在一起。我的问题是这个。为什么我的县菜单没有填充?我正在尝试将选定的状态id属性发布到ajax以获取具有相同ID的县。看不出有什么不对。
答案 0 :(得分:1)
当然,你不会得到你想要的东西,因为在你的PostVent.php
你正在展示的是id
而隐藏的价值是name
。它应该是:
echo '<option value="' . $row['id'] . '">' . $row['name'] . '</option>';
<option>
的语法为<option value='[Specifies the value to be sent to a server]'>[Label or display value]</option>
您的查询也应该是:
SELECT id, name FROM states ORDER BY name