Adding a panel field from a script

Hello,

I find it very hard to understand form fields in the panel, and how they are built with a php class and functions.

I am trying to make a custom form field called PaperSearchField. It should be a normal input field, but after any input a JSON request is made in an additional script. On success, I want to create a structure field in the panel form (and ideally: fill it).

$(document).ready(function() {
    var input = $('#form-field-title');
    input.on('input', function() { 
        getPapers(input.val());
    });
    
    function getPapers(inputValue) {
        if (inputValue.length > 3) {
            $.ajax({
                type: "GET",
                url: "../../../../assets/js/get_papers.php",
                data:{ query: inputValue}, 
                success: function(data){
                    // ADD A STRUCTURE FIELD HERE
                }
            })
        }
    }
});

I have tried to create an empty structure field, and fill it using html() like:

$(".structure-entries").html('<div id="structure-entry-CODE" class="structure-entry ui-sortable-handle">...</div>');

But this doesn’t seem to be the right way at all to create a new field in the panel (it doesn’t write it down in the page’s txt file). Can someone help?

Thanks a lot