Display name in input using autocomplete

Asked

Viewed 362 times

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 registration ID, I would like to make 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 it’s getting: inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

  • Is there any other code besides this?

  • You are talking about the code on the server that returns the json?

  • No, autocomplete javascript

  • There’s no, the only JS of the autocomplete I’m using is this.

2 answers

1


Try to do so by inserting the label input by his ID:

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

    It worked out, thank you very much! I had tried this way, but I didn’t use the false Return.

0

Which autocomplete are you using? How does it work? I am searching by username in an input field but returns only the name for value.

I believe it is the same thing that is happening with your problem that has been solved. When searching by USER in a new occurrence registration form, I can even return the user correctly, but I needed to save his id instead of the name. I am using the function "autocomplete/jquery.autocomplete.js"

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

complete.php page 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 when selecting it is the id in the input. if I change the first $reg['id'] for reg['nome'] name in the input but need to save id in the database.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.