<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); });