Using AJAX in Symfony
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);
}
}
php html ajax symfony twig
add a comment |
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);
}
}
php html ajax symfony twig
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
add a comment |
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);
}
}
php html ajax symfony twig
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
php html ajax symfony twig
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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.
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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