Display the name in the input using autocomplete

1

The autocomplete is working correctly, I get the following json:

[{"label":"Jorge Valdivia","value":"16"},{"label":"Vinicius Aquino","value":"15"}]

The problem is that when I select an option, the input receives the record ID, I would like to have it display the person's name in the input.

// -------------- Autocomplete --------------
    $('#search_broker').autocomplete({
        source: path + "User/get_brokers",
        minLength: 2,
        select: function (event, ui) {
            broker_name = ui.item.label;
            broker_id = ui.item.value;
            alert('Nome: ' + broker_name);
        }
    });

Look how you're getting:

    
asked by anonymous 17.07.2017 / 14:57

2 answers

1

Try to do this by inserting the label into the input by its ID:

$('#search_broker').autocomplete({
   source: path + "User/get_brokers",
   minLength: 2,
   select: function (event, ui) {
      $("#id_do_input").val(ui.item.label);
      return false;
   }
});
    
17.07.2017 / 15:45
0

What autocomplete are you using? How it works? I'm doing a username search in an input field but returns only the name for the value.

I think it's the same thing that's going on with your problem that was healed. When searching for USER in a new occurrence registration form, I can even return the user correctly, but I had to record his id instead of the name. I'm using the "autocomplete / jquery.autocomplete.js" function

$(document).ready(function(){
        $("#userb").autocomplete("completar.php?userb=", { 
            width:200,
            selectFirst: false
        });
    });

page complete.php like this:

//conecta no banco
include("conecta.php");

$q = $_GET["userb"];

$sql = "SELECT * FROM user WHERE nome like '%".$q."%'";

$query = mysqli_query($conn,$sql); // or die ("Erro". mysql_query());

while($reg=mysqli_fetch_array($query, MYSQLI_ASSOC)){

    echo $reg["id"]."|".$reg["nome"]."\n";

}

The way this looks for the name and selecting it is the id in the input. if I change the first $reg['id'] by reg['nome'] is the name in the input but I need to save the id in the bank.

    
28.08.2017 / 14:16