我想用一个AJAX响应函数填充一个select2下拉列表,并在其所需的下拉列表更改值时将调用该数据。我在控制台上登录以确保已实现该功能,但无论如何,该指令未由计算机执行:

create.tpl-保存前端代码

    $('.select2lead').select2({
        minimumInputLength: 3,
        ajax: {
            type: 'GET',
            url: '/modules/support/ajaxLeadSearch.php',
            dataType: 'json',
            delay: 250,
            data: function (params) {
                return {
                    term: params.term
                };
            },
            processResults: function (data) {
                return {
                    results: data,
                    more: false
                };
            }
        }
    });

    $('.select2lead').on("change", function() {
       var value = $(this).val();
        // console.log(value);
        searchProjectsByLeadID(value);
    });

    function searchProjectsByLeadID(id){
        $('.select2project').select2({
            minimumInputLength: 3,
            ajax: {
                type: 'GET',
                url: '/modules/support/ajaxProjectSearch.php',
                dataType: 'json',
                delay: 250,
                data: {
                    "id": id
                },
                processResults: function (data) {
                    return {
                        results: data,
                        more: false
                    };
                }
            }
        });
        console.log(id);
    }


ajaxProjectSearch.php

<?php
require_once('../../config.php');

$login = new Login();
if (!$login->checkLogin()) {
    echo lang($_SESSION['language'], "INSUFFICIENT_RIGHTS");
    exit();
}

$db = new Database();

// Select the projects
$query = "
                    SELECT
                        ProjectID AS project_id,
                        ProjectSummaryShort AS project_summary,
                    FROM
                        `rapports_projectTBL`
                    INNER JOIN LeadTBL ON rapports_projectTBL.LeadID=LeadTBL.LeadID
                    WHERE
                        ProjectID > 0
                    AND
                        rapports_projectTBL.LeadID LIKE :leadId
                    ORDER BY
                        ProjectID
                    ASC
                ";

$binds = array(':leadId' => $_GET['id']);
$result = $db->select($query, $binds);
$json = [];

foreach ($result as $row){
    $json[] = ['id'=>$row['project_id'], 'text'=>$row['project_summary']];
}

echo json_encode($json);


控制台中的网络输出

javascript - 使用AJAX从函数调用中填充Select2-LMLPHP
https://i.imgur.com/6ZmGGxa.png
*如我们所见,searchProjectsByLeadID(id)函数被调用,但没有任何回报。没有错误或任何值。唯一要运输的是第一个选择框,它会根据搜索框中键入的内容来获取潜在客户数据。 *

最佳答案

解决

通过使SQL查询查找LIKE而不是DIRECT MATCH来发出问题:

错误
哪里
                        项目ID> 0
                    和
                        rapports_projectTBL.LeadID喜欢:leadId

正确
哪里
                        rapports_ProjectTBL.LeadID =:leadId

10-04 19:40