使用AJAX从函数调用填充Select2

时间:2018-03-13 13:17:02

标签: javascript jquery mysql ajax

我想用一个带有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);

控制台中的网络输出

enter image description here https://i.imgur.com/6ZmGGxa.png *正如我们所看到的,searchProjectsByLeadID(id)函数被调用,但我们什么也得不到。没有错误也没有任何价值。传输的唯一方法是第一个选择框,它在搜索框中键入的内容中获取潜在客户数据。 *

1 个答案:

答案 0 :(得分:0)

<强>分辨

通过让SQL查询查找LIKE而不是DIRECT MATCH来调用问题:

WRONG
哪里                         ProjectID&gt; 0                     和                         rapports_projectTBL.LeadID LIKE:leadId

CORRECT
哪里                         rapports_ProjectTBL.LeadID =:leadId