4

I currently have a code working using the UI autocomplete and seems to display the correct data I get from my mysql DB. Now I want to complicate things by been able to accept more than 1 value. I have searched the internet up and down and still cannot fix my code in order to work, I am a newbie with jquery and specially with autocomplete. Here is what I have so far..

$('#companyautocomplete').autocomplete({
    source: function(request, response) {
        //separator: ' ',
        $.ajax({
            url: "company_search.php",
            dataType: "json",
            data: {
                name_startsWith: request.term,                        
                term: $('#companyautocomplete').val(),

            },
            success: function(data) {
                response($.map(data, function(item) {
                    return {
                        compid: item.compid,
                        label: item.value + ' (' + item.address + ', ' + item.city + ', ' + item.state + ', ' + item.zipcode + ')',
                        value: item.value,
                        address: item.address + ', ' + item.city + ', ' + item.state + ', ' + item.zipcode,
                        phone: item.phone,
                        problematic: item.problematic
                    }
                }))
            },  
        });
    },
    select: function(event, ui) {
        $('#companyautocomplete').val(ui.item.value);
        $('#companyid').val(ui.item.compid);
        $('#c_address').val(ui.item.address);
        $('#c_phone').val(ui.item.phone);
        if (ui.item.problematic!=1){
            $('#companyautocomplete').removeClass("ui-autocomplete-error");
            document.getElementById('Sendbutton').style.display="block";
        } else {
            $('#companyautocomplete').addClass("ui-autocomplete-error");
            document.getElementById('Sendbutton').style.display="none";
        }   
    }
});

This is the response I get from the company_search.php...

[{"compid":"36","value":"MCDONALD OF ALL SAINTS RD","address":"9261 all saints rd","phone":"","problematic":"0","zipcode":"20723","city":"Laurel","state":"MD","completeaddress":"9261 all saints rd, Laurel, MD, 20723"},{"compid":"37","value":"MCDONALD OF BOWIE","address":"4306 nw CRAIN HWY","phone":"","problematic":"0","zipcode":"20716","city":"Bowie","state":"MD","completeaddress":"4306 nw CRAIN HWY, Bowie, MD, 20716"}]

So far it all works as I want but it doesn't accept more than 1 value, how can I achieve this ? Thanks in advance !!

4
  • Have you seen this demo? jqueryui.com/demos/autocomplete/#multiple-remote Commented May 19, 2012 at 18:46
  • Yes I saw that demo and tested it but was not able to implement the other responses like "compid: item.compid", "phone: item.phone", etc. I added: response($.map(response, function(item) { return { compid: item.compid, label: item.value + ' (' + item.address + ', ' + item.city + ', ' + item.state + ', ' + item.zipcode + ')', value: item.value, address: item.address + ', ' + item.city + ', ' + item.state + ', ' + item.zipcode, phone: item.phone, problematic: item.problematic } })) Commented May 19, 2012 at 19:42
  • This, according to firebug, returns all the info but the menu never shows up nor I can select the value I want. Commented May 19, 2012 at 19:43
  • can you display you html for this question Commented Jan 24, 2014 at 4:53

1 Answer 1

7

Finally I found an answer to my problems !! This is the final code using multiple values/inputs with getting responses from a remote mysql DB.

$(function() {
    function split( val ) {
    return val.split( /,\s*/ );
    }
    function extractLast( term ) {
    return split( term ).pop();
    }

    $( "#companyautocomplete" )
    // don't navigate away from the field on tab when selecting an item
    .bind( "keydown", function( event ) {
    if ( event.keyCode === $.ui.keyCode.TAB &&
    $( this ).data( "autocomplete" ).menu.active ) {
    event.preventDefault();
    }
    })
    .autocomplete({
    source: function( request, response ) {
        $.getJSON( "company_search.php",{
            term: extractLast( request.term )},
            function( data ) {
            response( $.map( data, function( item ) {
                  return {
                        compid: item.compid,
                        label: item.value + ' (' + item.address + ', ' + item.city + ', ' + item.state + ', ' + item.zipcode + ')',
                        value: item.value,
                        address: item.address + ', ' + item.city + ', ' + item.state + ', ' + item.zipcode,
                        phone: item.phone,
                        problematic: item.problematic
                  }
            }));
        }
    );
    },
    search: function() {
        // custom minLength
        var term = extractLast(this.value);
        if (term.length < 3) {
            return false;
        }
    },
    focus: function() {
        // prevent value inserted on focus
        return false;
    },
    select: function( event, ui ) {
        $('#companyautocomplete').val(ui.item.value);
        $('#companyid').val(ui.item.compid);
        $('#c_address').val(ui.item.address);
        $('#c_phone').val(ui.item.phone);
        if (ui.item.problematic!=1){
            $('#companyautocomplete').removeClass("ui-autocomplete-error");
            document.getElementById('Sendbutton').style.display="block";
        } else {
            $('#companyautocomplete').addClass("ui-autocomplete-error");
            document.getElementById('Sendbutton').style.display="none";
        }
    }
});

});

Now it works like a charm. I hope this helps someone else looking for an answer.

Sign up to request clarification or add additional context in comments.

2 Comments

This was a big help to me in my project. I was able to take your code, modify if for my needs and get it doing exactly what I wanted it to. Thanks much!
insert value into two different input box

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.