我正在使用ajax获取数据并填充一个选择框。在以下代码中,我在ajax.php文件上回显输出,并将其接收。一切正常。但是,如您所见,所有html都填充在一个选择框中。我想返回两个单独的选择框而不是一个的数据。
因此,我的问题是,如何从ajax.php分别发送两种不同类型的数据,以及如何在成功函数中接收它。
$.ajax({
type: "POST",
url: "../ajax.php",
data: {name: 'select1', id: id},
cache: false,
success: function(html){
$(".select1").html(html);
}
});
Ajax.php仅回显数据:
//data for select1
foreach($rows as $row){
echo '<option>x</option>';
}
//I want to add another one for select2
foreach($rows1 as $row){
echo '<option>y</option>';
}
die();
并希望收到以下内容:
success: function(html){
$(".select1").html(data1);
$(".select2").html(data2);
}
编辑:我正在使用ajax来获取数据,因为我正在使用从属选择框并从数据库中获取数据。因此,通过单击select 1,它将从数据库中获取select 2的数据,这就是我这样做的原因。
最佳答案
您可以在Ajax.php中做类似的事情
//select1
$data['html1'] = NULL;
foreach($rows as $row){
$data['html1'] .= '<option>x</option>';
}
//select2
$data['html2'] = NULL;
foreach($rows1 as $row){
$data['html1'] .= '<option>y</option>';
}
$resonse = json_encode($data);
echo $response;
//JQUERY MAKE SURE THAT ITS OF TYPE JSON.
success: function(response){
$(".select1").html(response.html1);
$(".select2").html(response.html2);
}