我有两个多个下拉国家和运营商。运营商多选下拉列表将根据多个国家/地区下拉列表填充。只有当我从下拉列表中选择一个国家/地区时,它才有效,如果我选择多个国家/地区,它将不会显示其他所选国家/地区的选项。
html代码:
<select name="ind" class="form-control" id="select" multiple placeholder="Select Countries" >
<option value="0">Select Countries</option>
<?php $sqlfu=$con->prepare("select `id`,`country_name` from `countries` order by `country_name` ") or die(mysqli_error($con));
$sqlfu->execute()or die(mysqli_error($con));
$resfu=$sqlfu->get_result();
while($rowfu=$resfu->fetch_array())
{ $jind=$rowfu['id'];?>
<option value="<?php echo $jind;?>"><?php echo ucwords($rowfu['country_name']);?></option>
<?php } ?>
</select>
<select name="operators" class="form-control" id=operators" multiple placeholder="Select Mobile Operators" style="width:50%;"></select>
以下是我的ajax代码:
<script>
$("#select").change(function(){
var a=$(this).val();
$.ajax({
method:'post',
url:'operator.php',
data:{'id':a,'isAjax':true},
dataType:'json',
success:function(data)
{
var select=$("#sel"),options='';
select.empty();
for(var i=0;i<data.length;i++)
{
options +="<option value='"+data[i].id+"'>"+data[i].operator+"</option>";
}
select.append(options);
}
});
});
operator.php:
$id=$_POST['id'];
$a=implode("', '", $id);
$result=array();
$sql=$con->prepare("select `id`,`operator` from `operators` where `c_id`=?");
$sql->bind_param("s",$a);
$sql->execute() or die(mysqli_error($con));
$res=$sql->get_result();
while($row=$res->fetch_array())
{
$result[]=array('id'=>$row['id'],'operator'=>$row['operator']);
}
echo json_encode($result);
答案 0 :(得分:0)
在变量中收集多个id并传递给operator.php,如
var a = [];
$('#select').each(function() {
a.push($(this).val())
});
在爆炸爆炸ID
之后你已经内爆了id$pin1 = explode(",",$a);
$pin2 = sizeof($pin1);
for($i=0; $i<=$pin2; $i++)
{
$id = $pin1[$i];
write your query here for matching c_id with $id
}
之后你可以爆炸id并与循环中的c_id匹配。
答案 1 :(得分:0)
在operator.php的下面一行:
$sql=$con->prepare("select id,operator from operators where c_id=?");
它始终返回单个记录,所以我在循环中完成了它,如下所示
$result = array();
foreach ($id as $a) {
$sql = $conn->prepare("select id,operator from operators where c_id=?");
$sql->bind_param("s", $a);
$sql->execute() or die(mysqli_error($conn));
$res = $sql->get_result();
while ($row = $res->fetch_array()) {
$result[] = array('id' => $row['id'], 'operator' => $row['operator']);
}
}
$res->close();
$conn->close();
echo json_encode($result);
答案 2 :(得分:0)
问题在于你的Ajax调用。您正在 onChange 事件上调用服务器端脚本。您应该在用户选择多个国家/地区时收集多个值
var selectedValues = $("#select").val();
现在向您的Ajax URL发出HTTP请求。并使用下面的代码.....
$id=$_POST['id'];
$a=implode("', '", $id); //you must escape the string
$result=array();
$sql=$con->prepare("select `id`,`operator` from `operators` where `c_id` in('$a')");
$sql->bind_param("s",$a);
$sql->execute() or die(mysqli_error($con));
$res=$sql->get_result();
while($row=$res->fetch_array())
{
$result[]=array('id'=>$row['id'],'operator'=>$row['operator']);
}
echo json_encode($result);
您应该使用MySQL IN功能