Autocomplete typeahead store user id into hidden field

Hi guys, I’m trying to store the user id into a hidden field (something like value=“userid”) so i can use it when i need to submit the from.
I’ve got a autocomplete field which is working fine but together with first and last name it shows also the user id.

This is what i’ve done so far:

The HTML form input

<input class="typeahead form-control" type="text">

The Javascript code

$('input.typeahead').typeahead({
        source:  function (query, process) {
        return $.get('controllers/ctrl_prova.php', { query: query }, function (data) {
                console.log(data);
                data = $.parseJSON(data);
                return process(data);
            })
        
        }
    });

The PHP code

$select_user = mysqli_prepare($conn, "SELECT user_id, user_first, user_last FROM users WHERE user_first LIKE '%".$_GET['query']."%' LIMIT 10");
mysqli_stmt_execute($select_user);
mysqli_stmt_bind_result($select_user, $clientid, $clientfirstname, $clientlastname);
$json = [];
while (mysqli_stmt_fetch($seleziona_utente)) {
$json[] = $clientfirstname.' '.$clientlastname.' '.$clientid;

}
echo json_encode($json);

Many thanks

Hey,

Fair enough. What’s not working?
If data contains a user property you could probably just do $("#my-hidden-field").val(data.user); withing the source callback.

Hi @James_Hibbard thanks for your answer in the end i’ve sorted it out using jquery ui autocomplete instead of typeahed:

$(document).ready(function(){
var ac_config = {
source:‘controllers/ctrl_inbox_client_search.php’,
select: function(event, ui) {
$(‘#id_cliente’).val(ui.item.value);
event.preventDefault();
$(“#clienti”).val(ui.item.label); },
minLength:1
};
$(“#clienti”).autocomplete(ac_config);
});

I would like to take a chance to ask you something else, when i run thew query to the database i do it in this way:

SELECT user_id, user_first, user_last FROM user_group_join INNER JOIN users ON user_group_join . user_join_id = users . user_id WHERE user_group_join . group_join_id = 1 AND user_group_join . user_join_id NOT LIKE 59 AND users . user_first LIKE ‘%{$term}%’ OR users . user_last LIKE ‘%{$term}%’

I don’t understand why it still shows the user 59 do you have any clue?
Many thanks

No idea :slight_smile:

Try one of these suggestions maybe? Or ask in the database forum?

I believe it has to do with precedence
I don’t bother trying to remember, I use parentheses.

AND takes precedence over OR, so without out parentheses the query is “saying”

WHERE these three - OR this one

https://dev.mysql.com/doc/refman/5.7/en/operator-precedence.html

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.