user3139428
user3139428

Reputation: 83

Symfony, How to pass a variable to the path?

I need to pass the var 'busqueda' for the path, if instead of using the var I put text if it works, but to shove the variable tells me there.

JavaScript code in twig.

var busqueda = document.getElementById('search_keywords').value;

xmlhttp.open("GET","{{path('searchCorreos', {'page': thisPage, 'search': busqueda } )}}",true); 
xmlhttp.send();

Upvotes: 1

Views: 483

Answers (2)

Berry Ligtermoet
Berry Ligtermoet

Reputation: 881

If you run into this problem more often (needing to append javascript variables to symfony generated paths/urls) you can make use of the FOSJsRoutingBundle: https://github.com/FriendsOfSymfony/FOSJsRoutingBundle

This allows you to do the following in your javascript source:

var busqueda = document.getElementById('search_keywords').value;
var path = Routing.generate('searchCorreos', { page: 'thisPage', search: busqueda });

xmlhttp.open("GET", path, true);
xmlhttp.send();

Upvotes: 0

devilcius
devilcius

Reputation: 1884

Since it's a GET request, this should work:

var busqueda = document.getElementById('search_keywords').value;

xmlhttp.open("GET","{{ path('searchCorreos', {'page': thisPage}) }}&search=" + busqueda,true); 
xmlhttp.send();

Upvotes: 1

Related Questions