jquery autocomplete - adding link to json data
Hi,
I am using the following code for jquery autocomplete. Can anyone help me to add links to json data?
- <script src="//ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-3-typeahead/4.0.2/bootstrap3-typeahead.min.js"></script>
<script>
$(document).ready(function(){
$('#country').typeahead({
focus: function(event, ui) {
// prevent autocomplete from updating the textbox
event.preventDefault();
},
select: function(event, ui) {
// prevent autocomplete from updating the textbox
event.preventDefault();
// navigate to the selected item's url
window.open(ui.item.url);
},
minLength: 2,
cache: false,
cacheLength: 0,
source: function(query, result){
$.ajax({
url:"autocomplete.php",
method:"POST",
data:{query:query},
dataType:"json",
success:function(data)
{
result($.map(data, function(item){
return item;
}));
}
})
}
});
});
</script>
autocomplete.php looks like below:
- <?php
$q = mysqli_real_escape_string($connect, $_POST["query"]);
if(isset($q)){
$query = "SELECT * FROM companies WHERE company LIKE '%".$q."%' LIMIT 0,10";
$result = mysqli_query($connect, $query);
$data = array();
}
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
$data[] = $row['company'];
}
echo json_encode($data);
}
?>
Regards,
Naveen