Populate Select2 from function call with AJAX

104
March 13, 2018, at 5:44 PM

I want to populate a select2 dropdown with an AJAX response function with data that will be called whenever the dropdown required for it changes value. I console logged to be sure that the function gets reached, but nonetheless, the instructions are not being ran by the machine:

create.tpl - Holds front-end code

    $('.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);

Network output in console

https://i.imgur.com/6ZmGGxa.png *As we can see, the searchProjectsByLeadID(id) function gets called but we get nothing back. No errors nor any values. The only thing getting transported is the first select box, which fetches lead data upon whats typed in the searchbox. *

READ ALSO
Slick Slider z-index issues

Slick Slider z-index issues

I'm using slick slider and have created the following custom layout with it

171
Like/Unlike Button with jQuery in a Django App

Like/Unlike Button with jQuery in a Django App

I'm trying to create a Like/Unlike button with jQuery! Here's what I did,

130
&lt;input type="file&gt; limit selectable files by name

<input type="file> limit selectable files by name

I want to make an upload table where each file has its own upload ButtonTo make it easier for user i want to restrict the choosable files by name similar to how the accept tag limits the type

118
Jquery .slice chars from array?

Jquery .slice chars from array?

obj is a array that contains youtube linksIt includes around 32 youtube links if you are at youtube

136