0





27

Advertisement

I am trying to use JSON to suggest search items from the mysql database in php but when I type anything in the input box, all results from the database are showing and they are not being refined depending on the input text.

Here is my html/php UI page:

<!-- Start of content -->
<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script>
$(function() {
    $( "#searchText" ).autocomplete({
        source: 'includes/functions/json_search.php',
        minLength:'2'
    });
});
</script><div class="ui-widget">
    <label for="searchText">Search the menu: </label>
    <input id="searchText" name="searchText">
</div>

Here is my php:

<?php 
include_once ("../../config/init.php");//get search term
$searchTerm = $_GET['searchText'];//get matched data from menu table
$query = $connection->query("SELECT * FROM menu WHERE name LIKE '%".$searchTerm."%' ORDER BY name ASC");
while ($row = $query->fetch_assoc()) {
    $data[] = $row['name'];
}//return json data
echo json_encode($data);?>

can anyone suggest why it will not refine the results? thanks

Question author Smith-05-bird | Source

Advertisement


0


//get search term$searchTerm = $_GET['searchText'];

should be:

$searchTerm = $_GET['term'];

Per jqueryui autocomplete doc

The Autocomplete plugin does not filter the results, instead a query string is added with a term field, which the server-side script should use for filtering the results.

Answer author Dpp