Using AJAX in Symfony












-1














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);
}

}









share|improve this question
























  • You said "But my AJax code seem not to be working." Can you please elaborate with some sort of error you get?
    – Jovan Perovic
    Nov 20 '18 at 19:59
















-1














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);
}

}









share|improve this question
























  • You said "But my AJax code seem not to be working." Can you please elaborate with some sort of error you get?
    – Jovan Perovic
    Nov 20 '18 at 19:59














-1












-1








-1







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);
}

}









share|improve this question















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);
}

}





<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" />





<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" />






php html ajax symfony twig






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '18 at 20:00









Jovan Perovic

16.7k42967




16.7k42967










asked Nov 20 '18 at 19:26









Oshana Crypto

1




1












  • You said "But my AJax code seem not to be working." Can you please elaborate with some sort of error you get?
    – Jovan Perovic
    Nov 20 '18 at 19:59


















  • You said "But my AJax code seem not to be working." Can you please elaborate with some sort of error you get?
    – Jovan Perovic
    Nov 20 '18 at 19:59
















You said "But my AJax code seem not to be working." Can you please elaborate with some sort of error you get?
– Jovan Perovic
Nov 20 '18 at 19:59




You said "But my AJax code seem not to be working." Can you please elaborate with some sort of error you get?
– Jovan Perovic
Nov 20 '18 at 19:59












1 Answer
1






active

oldest

votes


















0














Are you sure that your Symfony controller returns a response (any type it is)?

You should replace echo json_encode($data) by return new JsonResponse($data)





Make sure the JsonResponse class has been loaded by your controller.






share|improve this answer























  • This really seems more like a question than an answer. Good way to get yourself down voted. The question does not contain nearly enough information to answer.
    – Cerad
    Nov 20 '18 at 22:54






  • 1




    I'm telling it because the controller generally end by returning a response not with echo function.
    – Mercury
    Nov 21 '18 at 8:15











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400180%2fusing-ajax-in-symfony%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Are you sure that your Symfony controller returns a response (any type it is)?

You should replace echo json_encode($data) by return new JsonResponse($data)





Make sure the JsonResponse class has been loaded by your controller.






share|improve this answer























  • This really seems more like a question than an answer. Good way to get yourself down voted. The question does not contain nearly enough information to answer.
    – Cerad
    Nov 20 '18 at 22:54






  • 1




    I'm telling it because the controller generally end by returning a response not with echo function.
    – Mercury
    Nov 21 '18 at 8:15
















0














Are you sure that your Symfony controller returns a response (any type it is)?

You should replace echo json_encode($data) by return new JsonResponse($data)





Make sure the JsonResponse class has been loaded by your controller.






share|improve this answer























  • This really seems more like a question than an answer. Good way to get yourself down voted. The question does not contain nearly enough information to answer.
    – Cerad
    Nov 20 '18 at 22:54






  • 1




    I'm telling it because the controller generally end by returning a response not with echo function.
    – Mercury
    Nov 21 '18 at 8:15














0












0








0






Are you sure that your Symfony controller returns a response (any type it is)?

You should replace echo json_encode($data) by return new JsonResponse($data)





Make sure the JsonResponse class has been loaded by your controller.






share|improve this answer














Are you sure that your Symfony controller returns a response (any type it is)?

You should replace echo json_encode($data) by return new JsonResponse($data)





Make sure the JsonResponse class has been loaded by your controller.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 21 '18 at 10:30









DarkBee

8,61042742




8,61042742










answered Nov 20 '18 at 21:02









Mercury

1




1












  • This really seems more like a question than an answer. Good way to get yourself down voted. The question does not contain nearly enough information to answer.
    – Cerad
    Nov 20 '18 at 22:54






  • 1




    I'm telling it because the controller generally end by returning a response not with echo function.
    – Mercury
    Nov 21 '18 at 8:15


















  • This really seems more like a question than an answer. Good way to get yourself down voted. The question does not contain nearly enough information to answer.
    – Cerad
    Nov 20 '18 at 22:54






  • 1




    I'm telling it because the controller generally end by returning a response not with echo function.
    – Mercury
    Nov 21 '18 at 8:15
















This really seems more like a question than an answer. Good way to get yourself down voted. The question does not contain nearly enough information to answer.
– Cerad
Nov 20 '18 at 22:54




This really seems more like a question than an answer. Good way to get yourself down voted. The question does not contain nearly enough information to answer.
– Cerad
Nov 20 '18 at 22:54




1




1




I'm telling it because the controller generally end by returning a response not with echo function.
– Mercury
Nov 21 '18 at 8:15




I'm telling it because the controller generally end by returning a response not with echo function.
– Mercury
Nov 21 '18 at 8:15


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53400180%2fusing-ajax-in-symfony%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Costa Masnaga

Fotorealismo

Sidney Franklin