<form action="test.php>" method="post"> <label for="anything">Что-нибудь</label> <div id="remote"> <input type="text" name="anything" class="typeahead" maxlength="64" required autofocus> </div><hr> <p><input type="submit" name="saveChanges" value="Сохранить изменения"></p> </form> 
 <script> var users = new Bloodhound({ datumTokenizer: Bloodhound.tokenizers.obj.whitespace('Зачем это вообще?'), queryTokenizer: Bloodhound.tokenizers.whitespace, remote: { url: 'autocomplete.php?text=%QUERY', wildcard: '%QUERY' } }); $('.typeahead').typeahead({ minLength: 1 }, { source: users }); </script> 

autocomplete.php

 <?php $text = $_GET['text']; $conn = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD); $sql = "SELECT id, nickname FROM users WHERE nickname LIKE '$text%' ORDER BY nickname DESC LIMIT 5"; $st = $conn->query($sql); $users = array(); foreach($st->fetchAll(PDO::FETCH_ASSOC) as $row) { $users[] = $row; } $conn = null; echo json_encode($users); ?> 

test.php

 <?php if(is_numeric($_POST['anything'])) echo "Всё работает"; ?> 

It is necessary that after clicking the button in $_POST id , not a nickname , but at the same time that nickname shown in input[name=anything] . Bourgeois write a lot of things, but there is either old or not working (for me). The most plausible (though also not working) solution:

 $('.typeahead').typeahead({ minLength: 1 }, { source: users }).on('typeahead:selected', function(event, data) { $('.typeahead').val(data.id); }); 

    1 answer 1

    The most suitable option for you in HTML is:

     <select name="userid"> <option value="id1">nickname1</option> <option value="id2">nickname2</option> </select> 

    As a result, the form will pass userid = idXXX. Naturally, before choosing a nickname, it is necessary that all nicknames and their id are filled in on the page.

    • This is not even considered: the user must write data and auto-complete, fetching data from the database, they must be substituted, otherwise I will have to do thousands of 'option'. - TrueNoob141
    • But this does not prevent you from making such a field hidden, and using ajax, get a list of nicks and id and dynamically fill in based on the entered text. After all, the user can write a nickname, which is not in the database, and what id you then hope to get? - Dmitry
    • I made a hidden text field and in the code above instead of $('.typeahead') simply substituted this field. Thanks for the tip. - TrueNoob141