使用 MySQL Ajax 填充多个下拉列表



我有两个多个下拉国家和运营商。运营商多选下拉列表将根据多个国家/地区下拉列表进行填充。仅当我从下拉列表中选择一个国家/地区时,它才有效,如果我选择多个国家/地区,它将不会显示其他选定国家/地区的选项。

网页代码:

<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);
        }
    });
});

操作员.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);

在运算符.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);

在变量中收集多个 id 并传递给运算符.php例如

     var a = [];
$('#select').each(function() {
    a.push($(this).val())
});

内爆后已内爆 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匹配。

问题出在你的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 函数

最新更新