简体   繁体   中英

Typeahead in a Model return empty value to form input field

I create a table with a button. When a user switch the button it will open a Bootstrap-Modal. Inside the Modal is a form wite a typeahed select field. The list works and all items are selectable.

My Problem: If i select a item it shows on form on post the return value (PHP $_POST) are empty.

My js code

$(document).ready(function () {
$('.typeahead').typeahead({
    source: function (query, process) {
        $.ajax({
            url: '/testPost',
            type: 'POST',
            dataType: 'JSON',
            //data: 'query=' + query,
            data: 'query=' + $('#contactName').val(),

            success: function(data)
            {
                var results = data.map(function(item) {
                    //var someItem = { contactname: item.ContactName, telephone: item.Telephone, email: item.Email };
                    var someItem = { contactname: item.ContactName, id: item.id };
                    console.log(item);
                    return JSON.stringify(someItem.contactname);
                });

                return process(results);
            }
        });
    },
    minLength: 1,
    updater: function(item) {
        // This may need some tweaks as it has not been tested
        var obj = JSON.parse(item);
        return item;
    }
});

});

php json

    $return_arr[] = array("id" => "1", "ContactName" => "Dooome");
$return_arr[] = array("id" => "2", "ContactName" => "Caa");
$return_arr[] = array("id" => "3", "ContactName" => "Veee");
$return_arr[] = array("id" => "4", "ContactName" => "Moo");
$return_arr[] = array("id" => "5", "ContactName" => "Reee");
#$return_arr[] = array("value" => "Dooome");

header("Content-Type: application/json");
print_r(json_encode($return_arr));

If you want select in typeahead input .val() not working, you get value with: $('#contactName').typeahead('val') , this way you get value of input for sending in data of Post Ajax

Look at the documentation of typeahead

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM