无法在select2中获取要呈现的远程数据

qqrboqgw  于 2021-06-17  发布在  Mysql
关注(0)|答案(0)|浏览(162)

我使用mysql数据库检索数据并将其显示在select2中。现在,搜索数据显示在get调用的预览中。但不在select2的选项中。即使收到正确的响应,也无法将其添加到选择框的选项中

下面是我的javascript代码

$("#selecter").select2({
    ajax: {
        url: "index.php",
        dataType: 'json',
        delay: 250,
        data: function (params) {
            return {
                q: params.term // search term
            };
        },
        processResults: function (data) {
            return {
                results: data
            };
        },
        cache: true
    },
    escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
    minimumInputLength: 2
});

下面是php代码

if(isset($_GET['q'])){
    $urlparam_name = $_GET['q'] ."%";
    $link = mysqli_connect('localhost', 'root', '', 'customerdatabase_13030') or die("Error " .mysqli_error($link));
    $sql = "
        SELECT `customer_13030`.`custID`, `customer_13030`.`name`
        FROM `customer_13030`
        WHERE `customer_13030`.`custID` like '$urlparam_name' OR    `customer_13030`.`name` like '$urlparam_name' 
        GROUP BY `customer_13030`.`custID` ASC
    ";
    $result = mysqli_query($link, $sql) or die("Error " .mysqli_error($link));
    $rows = array();
    while ($row = mysqli_fetch_assoc($result))
    {
        $rows[] =array(
            'id' => $row['custID'],
            'name' => $row['name']
        );
    }
    echo json_encode($rows);
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题