0

Whenever I input letter 'a' it suggesting all the name with letter 'a' in the name_table. I want to to make it suggest only the name that start with letter 'a'. How to fix this?.

index.php

<html><script type="text/javascript">
$(function() {
    var availableTags = <?php include('autocomplete.php'); ?>;
    $("#first_name").autocomplete({
        source: availableTags,
        autoFocus:true
    });
});
</script>
<form>
<label class="w3-label w3-text-green">Name</label>
<input class="w3-input w3-border" style="height: 30px" type="text" id="first_name" name = "name" required>
</form>
</html>

autocomplete.php

<?php
require('connection.php');
$sql = "select name from employee";
$result = mysqli_query($conn, $sql) or die("Error " .
mysqli_error($connection));

$dname_list = array();
while($row = mysqli_fetch_array($result))
{
$fullname = $row['name'];

$dname_list[] = $fullname;
}
echo json_encode($dname_list);
?>
M. Eriksson
  • 13,450
  • 4
  • 29
  • 40
Tekis
  • 1
  • 1
  • 1
  • obviously you're using a plugin, please share which plugin it is. I believe that in its manual you'll find the answer. – Ofir Baruch Feb 01 '17 at 06:15
  • _Suggestion:_ Always start by searching for your issue. When I Googled "jquery autocomplete start with", I got several solutions straight away... – M. Eriksson Feb 01 '17 at 06:19

2 Answers2

0

I suggest you to use ajax with PHP for getting the autocomplete results on keyup(when some key is pressed and released, JS provides this function).

This is pretty simple and straighforward tutorial on PHP-AJAX-Autocomplete http://papermashup.com/jquery-php-ajax-autosuggest/

0

In your autocomplete.php file try modifying your query

1.To find the word starting with a

query : SELECT name from employee where name like 'a%'

2.To find the word ending with a

query : SELECT name from employee where name like '%a'

3.To find the word containing a

query : SELECT name from employee where name like '%a%'

You have to use the like operator basically.In sql to match a particular word in your query result we use the below pattern :

%word/lettertosearch%

For more refer to this link : http://www.w3schools.com/sql/sql_like.asp

5eeker
  • 1,016
  • 1
  • 9
  • 30
  • The OP is using the jquery autocomplete plugin, where you fetch all the workds from the DB and letting the plugin do the matching, so the DB-code is correct. – M. Eriksson Feb 01 '17 at 06:29
  • As per the code provided, in the autocomplete.php he is returning all the name columns irrespective of the matching first letter. @Tekis wants in the result only the match starting with initial letter. Hence he will have to modify the query to get the desired result. – 5eeker Feb 01 '17 at 06:33
  • The OP is fetching all the results and the letting the plugin do the matching, all in the front end, witch is a valid way of doing it. That removes the overhead of a bunch of round trips to the DB. It's the JS-code that needs changing, (which is matching the already fetched list of words) just like both "Possible duplicate"-links suggests. – M. Eriksson Feb 01 '17 at 06:36
  • @MagnusEriksson I don't think so, in the html code in the available tags part the source for the data is fetched from the autocomplete.php, this file will trigger the result based on the query executed, hence he will have to modify the query. the jqueryui autocomplete just displays the result in the dynamic div giving the feel of autosuggestion, but the result is fetched from the autocomplete.php file. Tekis has modified the default behaviour and has given the source of the tags to php file instead of the standard data which comes from the jqueryui autocomplete sample code. – 5eeker Feb 01 '17 at 06:43
  • If `source` is an array (which it is in the OP's exampe), it does a search, using that list. What the OP want's is a callback function that filters that list to only fetch those starting with the search-term. If you don't have too many words, this is very much the recommended way of doing it, since it saves resources in the backend. – M. Eriksson Feb 01 '17 at 06:48
  • @MagnusEriksson yes that is correct. But in this case the source is a backend file which is reading the values from the DB. So get the required result from DB you will have to make a query like that. – 5eeker Feb 01 '17 at 06:50
  • That back end file is only generating a json array with all the words, so when the page is parsed in PHP, it will be the same as hard coding that array: `var availableTags = ['word1', 'word2', ...];` It's not invoked/called when the plugin is used in the FE. The plugin doesn't know _where_ that data comes from. – M. Eriksson Feb 01 '17 at 06:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/134552/discussion-between-adi-and-magnus-eriksson). – 5eeker Feb 01 '17 at 06:56