I am trying to use "typeahead" function with Symfony. But my AJax code seem not to be working. Can you guys point me out to the fault?I searched in the internet many times and all most all the questions asked regarding this has not being answered well. Please if someone can arrange these to work in symfony 2.8, it will be a great help to lot of people.Thank you
My Twig code
<script>
$(document).ready(function(){
$('#country').typeahead({
source: function(query, result)
{
$.ajax({
url:"{{path('fetch')}}",
method:"POST",
data:{query:query},
dataType:"json",
success:function(data)
{
result($.map(data, function(item){
return item;
}));
}
})
}
});
});
</script>
<input type="text" name="country" id="country" class="form-control input-lg" autocomplete="off" placeholder="Type Country Name" />
My controller
/**
* @Route("/fetch", name="fetch")
*/
public function preAllAction(Request $request)
{
//fetch.php
$connect = mysqli_connect("localhost", "root", "root", "galleit");
$request = mysqli_real_escape_string($connect, $_POST["query"]);
$query = "SELECT * FROM products WHERE name LIKE '%".$request."%'";
$result = mysqli_query($connect, $query);
$data = array();
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_assoc($result))
{
$data[] = $row["name"];
}
echo json_encode($data);
}
}
</div>