I'm using materialize autocomplete to get suggestion list from my database table. I give the source as php file in which I'm echoing the json file. Below is the code. It's not working. why?
index.php
<div class="input-field ">
<input type="text" name="t" id="t" class="autocomplete">
</div>
<script>
$(function () {
$('input.autocomplete').autocomplete({
source: 'suggest.php?key=%QUERY'
});
});
</script>
suggest.php
<?php
$key=$_GET['key'];
$array = array();
$conn = mysqli_connect('localhost', 'root', '', 'home_services');
$query= "select * from worker where lname LIKE '%{$key}%'";
$res = mysqli_query($conn, $query);
if($res->num_rows>0){
while($row=$res->fetch_assoc()){
$lname[]= trim($row["lname"]);
}
}
echo json_encode($lname);
?>