0

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

1
  • Use ajax for batter response Commented Dec 22, 2016 at 14:41

1 Answer 1

1
//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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.