How to Ajax with select2 auto fill another input field

556 Views Asked by At

please your help ,how to auto fill with ajax + select2 using medoo framework database to autofill another input in same form . this my ajax code :

$('.matrix').select2({
    ajax: {
        url: "index.php",
        dataType: 'json',
        data: function (params,page) {
            return {
                q: params.term, // search term
                qa: 'matrix'
            };
        },
        processResults: function (data,params) {
            return {
                results: $.map(data, function(obj) {
                    return { id: obj.id, text: obj.text };
                })
            };
        },
        //cache: true,
    },
    minimumInputLength: 3,
    placeholder: "<?php _e('Please Select'); ?>",
});

Please your advice .

1

There are 1 best solutions below

0
On

This my search data :

case "matrix":
    $searchstring = "";
    if(isset($_GET['q'])) $searchstring = $_GET['q'];

    if($searchstring != "") {
        $items = $database->select("tbl_matrix", "*", [ "OR" => [
            "bc_name[~]" => $searchstring

        ]]);

    } else {
        $items = $database->select("tbl_matrix", "*");
    }

    $results = array();
    $results[0]['id'] = 0;
    $results[0]['text'] = __('None');


    $i = 1;
    foreach($items as $item) {
        $results[$i]['id'] = $item['id'];
        $results[$i]['text'] = $item['bc_code']." ".$item['bc_name'];
        $i++;
    }

    echo json_encode($results);
    break;