我希望选择框的选定值从数据库中提取数据并在另一个选择框中填充
test.php包含显示:
<form role="form"> <div class="form-group"> <label
for="division_1">Fruits</label> <select id="division_1"
class="form-control division_1" name="division_1">
<?php $q = mysqli_query($dbc,"SELECT * FROM division_tbl");
while($data = mysqli_fetch_assoc($q)){
?> <option value="<?php echo $data['id'] ?>">
<?php echo $data['division_name'] ?></option>
<?php } ?>
</select> <div class="form-group"> <label for="block">Block</label>
<select id="block" class="form-control fruits" name="block">
</select> </div> </form>
这是jquery js文件
$(".division_1").change(function(){ var division_1 =
$('select[name=division_1]'); var data = 'division_1=' +
division_1.val();
$.ajax({ url: "config/ajax/send_value.php", type: "POST", data:
data, success: function(data) { Blocks(); } } }); return false;
});
//blocks
function Blocks(){
$('#block').empty();
$('#block').append("Loading");
$.ajax({
type: "POST",
url: "config/ajax/send_value.php",
contentType:"application/json; charset=uft-8", dataType:"json",
success: function(data){
$('#block').empty();
$('#block').append("Select block");
$.each(data,function(i, item){
$('#block').append(''+ data[i].name
+''); });
}, complete: function(){ } });
}
});
这是php帖子和接收文件
if (isset($_POST['division_1'])){
$q = mysqli_query($dbc,"SELECT * FROM block_tbl WHERE id =
'$_POST[division_1]'");
if (mysqli_num_rows($q)) {
$data = array();
while ($rows = mysqli_fetch_assoc($q)){
$data[] = array( 'id' => $rows['id'], 'name' => $rows['block_name']);
}
header('Content-type: application/json'); echo json_encode($data);
}
}