How to auto populate jquery ajax with select2

170 Views Asked by At

I want to my select2 after succes they populate other field .

I am using medoo framework .

this is my 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) {
                        $("#kordinat").val(data['id']);
                        return { id: obj.id, text: obj.text };

                    })
                };
            },
            //cache: true,
        },
        minimumInputLength: 3,
        placeholder: "<?php _e('Please Select'); ?>",
    });

and this is my code to call 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;

how to my code auto populate another field ? thank you .

0

There are 0 best solutions below