我在下面创建了这段代码,根据第一个下拉列表(部门)填写第二个下拉列表用户这是我的Ajax请求
<script type="text/javascript">
$(document).ready(function(){
$("#sel_depart").on('change', function(){
var deptid = $(this).val();
$.ajax({
url: 'action.php',
type: 'post',
data: {depart:deptid},
dataType: 'json',
success:function(response){
var len = response.length;
$("#sel_user").empty();
for( var i = 0; i<len; i++){
var id = response[i]['id'];
var name = response[i]['name'];
$("#sel_user").append("<option value='"+id+"'>"+name+"</option>");
}
}
});
});
});
</script>
这是第一次下拉
<tr>
<td>departments/td>
<td>
<select id="sel_depart" ><?php foreach($myrow2 as $row){?>
<option value="<?php echo $row['id'] ?>"> <?php echo $row['ename']?></option> <?php }?>
<div class="clear"></div>
</select>
</td>
</tr>
这是第二个
<tr>
<td>users</td>
<td>
<select id="sel_user">
<option value="0">- Select A user-</option>
</select>
</td>
</tr>
Ajax向action.php发送包含以下内容的请求:
if (isset($_POST['depart'])){
$departid = $_POST['depart']; // department id
if($obj->spinner2($departid)){
header("location:merchant.php?msg=district fetched ");
}
}
这是函数spinner2:
public function spinner2($departid){
$sql="SELECT id,ename FROM district where city_id =".$departid ;
$arr = array();
$result = mysqli_query($this->con,$sql);
while ($row = mysqli_fetch_array($query)){
$userid = $row['id'];
$name = $row['ename'];
$arr[] = array("id" => $userid, "name" => $name);
}
$users_arr=json_encode($arr);
echo $users_arr;
return $users_arr;
}
当我选择一个部门时没有任何事情发生